Topic: Object initialization.
Author: shashank@sashimi.wwa.com (Shashaanka)
Date: 1995/07/28 Raw View
bartee@rocky.contex.com (Subramania Natarajan) wrote:
>Hi.
> I have a question on C++ object initialization. Objects are initialized
>by constructors.
>Consider the following code:
>---------------------------
>#include <iostream.h>
>class X {
> public:
> X(const X&);
> X(X&);
> X(int a);
> int getI() const { return i_; }
>
> private:
> int i_;
>};
>X::X(const X& a) {
> cerr << "Copy constructor\n";
> i_ = a.getI();
>}
>X::X(X& a) {
> cerr << "Not Copy constructor\n";
> i_ = a.getI();
>}
>X::X(int a) {
> cerr << "Integer constructor\n";
> i_ = a;
>}
>const X functionReturnsX()
It should be
const X & functionReturnsX()
This is as per the language definition. Though most compiler will
probably give only the warning.
See what you get now.
>{
> cerr << "Function returns X\n";
> const X a(5); -------------> (2)
> return a;
>}
>main() {
> X b = functionReturnsX(); --------------> (1)
> return 0;
>}
>As you all know, at the statement marked (1), the copy constructor
>is called for 'b' when 'b' is initialized. Notice that class X has
>two copy constructors, the classic definition
> X::X(const X&)
>and the non-standard X::X(X&)
>Now, irrespective of the type qualification of the return value of the function
>'functionReturnsX' (ie, irrespective of whether it is 'X' or 'const X') the
>copy constructor that gets called for 'b' actually depends on the type
>qualification of the local variable 'a' defined within function
>'functionReturnsX'.
>ie.
>At the statement marked (2), if I had declared 'a' to be
>X a(5), then at statement (1), the copy constructor X::X(X&) is called
>for 'b'. This happens even if the return type of 'functionReturnsX' is
>'const X'.
>Similarly, at the statement marked (2), if I had declared 'a' to be
>const X a(5), then at statement (1), the copy constructor X::X(const X&)
>is called for 'b'. This happens even if the return type of
>'functionReturnsX' is just 'X' and NOT 'const X'.
>This leads me to believe that it is not the return type of a function,
>but the type of the variable that is returned which is used for exact match of
>arguments in the copy constructor.
>Is this right ??
>I tried this on the cfront CC of SGI on IRIX 5.3.
>An associated question is:
>-------------------------
> Is there any meaning to the 'const' qualification if the return
> value is an object (and NOT a pointer or reference to an object).
> const <Type> function( ..... );
> ^
> |
> Is this redundant and meaningless?? Infact, I get the message
> "Type qualification is redundant and meaningless in this definition."
>as a warning on my compiler.
>I would appreciate it, if you could respond to the address
> bartee@contex.com
>Thanks
>Bharathi.
Author: bartee@rocky.contex.com (Subramania Natarajan)
Date: 1995/07/13 Raw View
Hi.
I have a question on C++ object initialization. Objects are initialized
by constructors.
Consider the following code:
---------------------------
#include <iostream.h>
class X {
public:
X(const X&);
X(X&);
X(int a);
int getI() const { return i_; }
private:
int i_;
};
X::X(const X& a) {
cerr << "Copy constructor\n";
i_ = a.getI();
}
X::X(X& a) {
cerr << "Not Copy constructor\n";
i_ = a.getI();
}
X::X(int a) {
cerr << "Integer constructor\n";
i_ = a;
}
const X functionReturnsX()
{
cerr << "Function returns X\n";
const X a(5); -------------> (2)
return a;
}
main() {
X b = functionReturnsX(); --------------> (1)
return 0;
}
As you all know, at the statement marked (1), the copy constructor
is called for 'b' when 'b' is initialized. Notice that class X has
two copy constructors, the classic definition
X::X(const X&)
and the non-standard X::X(X&)
Now, irrespective of the type qualification of the return value of the function
'functionReturnsX' (ie, irrespective of whether it is 'X' or 'const X') the
copy constructor that gets called for 'b' actually depends on the type
qualification of the local variable 'a' defined within function
'functionReturnsX'.
ie.
At the statement marked (2), if I had declared 'a' to be
X a(5), then at statement (1), the copy constructor X::X(X&) is called
for 'b'. This happens even if the return type of 'functionReturnsX' is
'const X'.
Similarly, at the statement marked (2), if I had declared 'a' to be
const X a(5), then at statement (1), the copy constructor X::X(const X&)
is called for 'b'. This happens even if the return type of
'functionReturnsX' is just 'X' and NOT 'const X'.
This leads me to believe that it is not the return type of a function,
but the type of the variable that is returned which is used for exact match of
arguments in the copy constructor.
Is this right ??
I tried this on the cfront CC of SGI on IRIX 5.3.
An associated question is:
-------------------------
Is there any meaning to the 'const' qualification if the return
value is an object (and NOT a pointer or reference to an object).
const <Type> function( ..... );
^
|
Is this redundant and meaningless?? Infact, I get the message
"Type qualification is redundant and meaningless in this definition."
as a warning on my compiler.
I would appreciate it, if you could respond to the address
bartee@contex.com
Thanks
Bharathi.