2

1) what is the return value of following statement:

obj.classX::classX();

2) Another question regarding constructors in C++:

 classX(); 

creates an object. What is the expanded code generated by the compiler?

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
theneuronarc
  • 81
  • 1
  • 4
  • First statement is illegal, you cannot call a constructor. Don't do it. And there is no "standard compiler", so what code gets generated by a compiler depends totally on the compiler. Generally, whatever code is needed to invoke the constructor and then the destructor. – GManNickG Aug 01 '10 at 19:42

3 Answers3

4

Constructors do not return a value. Just like a function returning void.

for your 2nd question, the compiler will call the constructor, then the destructor.

tenfour
  • 36,141
  • 15
  • 83
  • 142
  • i tried printing the value of mVarX using following statement: (obj.classX::classX();).mVarX & it is printing correct value! I am not able to understand this behavior. – theneuronarc Aug 01 '10 at 17:16
  • 1
    This temp variable will go out of scope at the ; – Mike Aug 01 '10 at 17:25
3
  1. It doesn't have a return value. Therefore, it would be treated as void. You can't actually call the constructor that way though, so it matters little.
  2. That's compiler dependent of course, but in general it will allocate memory on the stack, and call the constructor to construct the object on the stack. Then the destructor will be called and the memory will be returned to the stack.
Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
  • 1) Do u mean obj.classX::classX(); is not valid? I tried this statement & Its calling constructor. – theneuronarc Aug 01 '10 at 17:19
  • 1
    What do you mean its compiler dependent. Looks well defined to me. – Martin York Aug 01 '10 at 17:49
  • 1
    @Martin York: The standard says very little about where things are actually stored. You could implement C++ on a platform that did not have any sort of hardware stack. It specifies only that the object would be constructed in the same place other automatic variables would be constructed. Exactly where that is, however, is not defined by the standard. – Billy ONeal Aug 01 '10 at 18:37
  • @Billy ONeal: What the standard does say is that an object will be created. This means the constructor will be called and the destructor will be called. – Martin York Aug 01 '10 at 20:15
  • @Billy @Marin: I think the confusion is the way you're interpreting the question: "What is the expanded code generated by compiler?" Billy is focused on the 'code generation' part, which is obviously compiler dependant, while Martin is focusing on the 'what will happen' part, which is different from 'how' and compiler independant. I think Billy's approach is more "correct" per se, but both directions can be implied by the question. – GManNickG Aug 02 '10 at 02:50
  • @Martin York: Yes, that is correct. An object will be created and then destroyed -- that is well defined. The compiler/machine dependent part is from where the memory for that object is obtained. For 99% of machines, that will be the hardware stack, but it does not *have* to be. – Billy ONeal Aug 02 '10 at 03:23
  • @theneuronarc: Sorry, I missed that comment somehow. Yes, what I mean is that the first statement you posted is not valid. – Billy ONeal Aug 02 '10 at 03:28
0
obj.classX::classX();

This is compile time error.

classX();

This creates a temporary instance of classX that is destroyed right at the end of the statement, IOW at the semicolon.

wilx
  • 17,697
  • 6
  • 59
  • 114
  • obj.classX::classX(); is not giving me any compilation errors and its calling constructor. I am using MSVS2005. – theneuronarc Aug 01 '10 at 18:24
  • 1
    @theneuronarc: So VC8 compiles this. And what's your point? That VC isn't the most standard conforming compiler around? We knew that. `:)` – sbi Aug 01 '10 at 18:31
  • 1
    @theneuronarc: Nevertheless it is not valid C++. Try the same on codepad.org (e.g. http://codepad.org/T9zkz2Dn) or with Comeau (http://www.comeaucomputing.com/tryitout/). It will not compile. – wilx Aug 01 '10 at 19:11
  • @theneuronarc: Compilers don't determine the language, the standard does. You cannot call the constructor, period. You may use placement-new which will construct an object a location, though. – GManNickG Aug 01 '10 at 19:41