Taking address of constructors??

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Taking address of constructors??

 
0
  #1
Aug 27th, 2005
Having return types of constructor means taking address of constructor(which is illegal)...can u explain me this....
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,576
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 709
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Taking address of constructors??

 
0
  #2
Aug 27th, 2005
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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: Taking address of constructors??

 
0
  #3
Aug 27th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,576
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 709
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Taking address of constructors??

 
0
  #4
Aug 27th, 2005
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: Taking address of constructors??

 
0
  #5
Aug 27th, 2005
Originally Posted by Narue
>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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,576
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 709
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Taking address of constructors??

 
1
  #6
Aug 27th, 2005
>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:
  1. 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:
  1. 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:
  1. std::string ( s, "This is a test" );
  2.  
  3. 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:
  1. 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:
  1. test::test()
  2. {
  3. // Try to initialize
  4. if ( failure )
  5. throw fatal_error;
  6. }
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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC