On 8/7/06, Gopi Krishna Komanduri <[EMAIL PROTECTED]> wrote:
Hi Paul Herring/All ,
       Thanks for your reply . But why it is not allowed.

Because The Standard (See links section of C-Prog if you don't have one yet) says so. To try and rationalise what a particular compiler does when presented with a construct that is not allowed is pointless.

What made me to think is , the memory for function will be allocated in code segment and the memory for class is in stack (I think so , please correct me if I am wrong).

You're wrong. Code Segments and Stacks (outside of the data construct provided for in C++) do not need to exist for C++ to function.

and moreover , if we want to declare a variable to a function , we can do with function prototype like
typedef void (*fun)();
fun x;
       But here we are not doing ;like that . So obviously , the available data type for compiler is class itself and I can create an object. Please correct my concept  if I am worng.

Your problem in the code as originally supplied is the ambiguity between a global function called x() which has the address x, and the class x which can be used to define an instance of that class.

You are not allowed to do this, since the compiler is not required to differentiate between them given the construct:

   x t;

You can probably disambiguate this by prefixing it with the word class (as I (I think, wrongly) suggested in my original answer.) You shouldn't be doing it to begin with.


Peter and/or Victor will be along in a few hours to put me right where I've gone wrong (but I don't think I have)... It's just not something I'd do in real-live code.


Paul Herring <[EMAIL PROTECTED] > wrote:
On 8/7/06, Gopi Krishna Komanduri <[EMAIL PROTECTED] > wrote:
This is the question I was asked in an interview.

You'll be wanting to quote from the Standard then.

ISO/IEC14882:1998

13 Overloading

When two or more different declarations are specified for a single name in the same scope, that name is said to be overloaded. Bu extension, two declarations in the same scope that declare the same name but with different types are called overloaded declarations. Only function declarations can be overloaded; object and type declarations cannot be overloaded.
 
In other words, it's not allowed. (Hope I've got the right part of the standard here...)

Paul Herring < [EMAIL PROTECTED] > wrote:
On 8/7/06, Gopi Krishna Komanduri <[EMAIL PROTECTED] > wrote:

Hi All,
           I have a class with name x and also a function ( independent of class) with same name. In main function I tried to declare an object for my class ( I thought we cann't assaign variable for a function with out declaring a prototype with typedef for a needed syntax). But I am getting error. Could any one of you explain what is the priority for memory allocation .
#include "stdafx.h"

void x()
{
    cout<<"Hai";
};
class x
{
public:
    x()
    {
    }
};
int main(int argc, char* argv[])
{
    x t;

class x t;

    return 0;
}


Why have you got a global function with the same name as a global class in the first place??

--
PJH

Into motorcycles? Need parts? Try www.gissit.com to contact lots of motorcycle breakers - free to use (UK based at the moment) __._,_.___

To unsubscribe, send a blank message to <mailto:[EMAIL PROTECTED]>.




SPONSORED LINKS
C programming language Programming languages Computer programming languages
Java programming language


YAHOO! GROUPS LINKS




__,_._,___

Reply via email to