Having return types of constructor means taking address of constructor(which is illegal)...can u explain me this....

Please don't start a new thread if your question isn't answered immediately. That said, there's no point in explaining the intricacies of a C++ implementation when you're so new to C++. Just take it on faith that there's a good reason, and expect to learn that reason when the time is right. You have more important things to learn about C++ for the moment. :)

yaa i do agree with u partially but isn't it wrong to skip things and moving on ....i just want my foundations to be strong....that is why i started a new thread

>but isn't it wrong to skip things and moving on
Sometimes you have to have the big picture before delving into the details. In this case, whether you know or not is irrelevant because it doesn't apply to practical use of the language. Now, if you were writing a compiler, I would go into more detail for you, because then it's relevant information.

>but isn't it wrong to skip things and moving on
Sometimes you have to have the big picture before delving into the details. In this case, whether you know or not is irrelevant because it doesn't apply to practical use of the language. Now, if you were writing a compiler, I would go into more detail for you, because then it's relevant information.

Hey... i am not here for any arguments....and i am sure that you are having much much more experience than me.......
I was just asking for a simple answer( I think i'll be able to understand that)

But anyways thanx..... u don't have to reply if u don't want to.....i'll try to search for it myself

>Hey... i am not here for any arguments....
Neither am I, but if you really want to be a jerk about it I'll be happy to oblige.

>I was just asking for a simple answer
Duh! But the answer isn't simple, nor is it relevant to your current studies, that's the whole point.

>u don't have to reply if u don't want to.....
Fine. The constructor does two things: it allocates memory to an object, and initializes the data members of the object to a predictable state. The latter is best defined by the program, but the former is only possible if the C++ run-time handles it. The reason that constructors do not return a value is because to do so would destroy any chance of using temporary objects without a variable to store them, and since the primary desire for a return value is to indicate success or failure, exceptions are the better solution.

Now, you could trick yourself into thinking that a constructor returns the object that it creates, if you really wanted to, because some of the syntax suggests it:

std::string s = std::string ( "This is a test" );

But that's just syntactic sugar. A more accurate description would be that a constructor evaluates to a temporary object, then that object is assigned to an identifier in the symbol table if an "assignment" is made during the declaration. If a constructor really returned the object that it created, this would be illegal, or a special case:

std::string s ( "This is a test" );

Sure, the language could have specified that a constructor returns, say, a bool, which can be used to determine success or failure, but then how would you link the newly created object with an identifier? C++ would be forced to either do something decidedly un-C++-like:

std::string ( s, "This is a test" ); 

std::cout<< s <<'\n';

Or force the Java approach where object variables are actually pointers and a special variant of the new operator returns the new object after checking the return value of the constructor and throwing an exception on failure:

std::string *s = new std::string ( "This is a test" );

In reality, the C++ approach is more flexible, and if you need to determine success or failure, simply throw an exception from within the constructor and call it good:

test::test()
{
  // Try to initialize
  if ( failure )
    throw fatal_error;
}

In light of this, there's no reason for a constructor to return a value and forcing a constructor to return a value would break code that assumes it evaluates to a temporary object, or require massive changes to the language just to keep everything working nicely, which of course would break millions of lines of existing C++ code.

Let's go back to your initial question, which was incorrectly answered for you and caused you to ask a less coherent question. Why doesn't a constructor return a value? Because it was a design decision when C with Classes was first created. For this and other implementation reasons, a constructor has no address that's meaningful to you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.