944,189 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2618
  • C++ RSS
Aug 27th, 2005
0

Taking address of constructors??

Expand Post »
Having return types of constructor means taking address of constructor(which is illegal)...can u explain me this....
Similar Threads
SpS
Reputation Points: 70
Solved Threads: 32
Posting Pro
SpS is offline Offline
598 posts
since Aug 2005
Aug 27th, 2005
0

Re: Taking address of constructors??

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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Aug 27th, 2005
0

Re: Taking address of constructors??

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
SpS
Reputation Points: 70
Solved Threads: 32
Posting Pro
SpS is offline Offline
598 posts
since Aug 2005
Aug 27th, 2005
0

Re: Taking address of constructors??

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Aug 27th, 2005
0

Re: Taking address of constructors??

Quote 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
SpS
Reputation Points: 70
Solved Threads: 32
Posting Pro
SpS is offline Offline
598 posts
since Aug 2005
Aug 27th, 2005
1

Re: Taking address of constructors??

>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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: muliplying without using a * operator!
Next Thread in C++ Forum Timeline: Can we access Databases through C or C++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC