| | |
Taking address of constructors??
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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.
>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.
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.
•
•
•
•
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
>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:
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:
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:
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:
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:
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.
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)
std::string s = std::string ( "This is a test" );
C++ Syntax (Toggle Plain Text)
std::string s ( "This is a test" );
C++ Syntax (Toggle Plain Text)
std::string ( s, "This is a test" ); std::cout<< s <<'\n';
C++ Syntax (Toggle Plain Text)
std::string *s = new std::string ( "This is a test" );
C++ Syntax (Toggle Plain Text)
test::test() { // Try to initialize if ( failure ) throw fatal_error; }
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.
![]() |
Similar Threads
- Why constructors don't have return types (C++)
- Question for Narue. (C)
- Computer taking IP addresses from Radio (Networking Hardware Configuration)
Other Threads in the C++ Forum
- Previous Thread: muliplying without using a * operator!
- Next Thread: Why constructors don't have return types
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings struct temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






