class instantiation question

H

hill.liu

Hi,

I stuck into this problem that I can't figure it out.
Here is the class definition:

class ctest {
public:
ctest(void) { cout << "ctest default constor" << endl; };
ctest(ctest& c) { cout <<"ctest copy constr" << endl; };
ctest(int a) { cout <<"ctest int constor" <<endl; };

ctest& operator= (ctest& a) { cout << "ctest copy assignment" <<
endl; return *this; };
operator int() { cout << "call operator int()" <<endl; return 20; }
};

int main(void)
{
ctest cc = ctest();
}

And it outputs as following:
ctest default constor
call operator int()
ctest int constor

I wonder why it will invoke "operator int()" and call "constructor
with int as argument".

Thanks
Brad
 
E

Elias Salomão Helou Neto

Hi,

I stuck into this problem that I can't figure it out.
Here is the class definition:

class ctest {
        public:
                ctest(void) { cout << "ctest default constor" << endl; };
                ctest(ctest& c) { cout <<"ctest copy constr" << endl; };
                ctest(int a) { cout <<"ctest int constor" <<endl; };

                ctest& operator= (ctest& a) { cout << "ctest copy assignment" <<
endl; return *this; };
                operator int() { cout << "call operator int()" <<endl; return 20; }

};

int main(void)
{
  ctest cc = ctest();

}

And it outputs as following:
ctest default constor
call operator int()
ctest int constor

I wonder why it will invoke "operator int()" and call "constructor
with int as argument".

Thanks
Brad

I wonder where is the

return( 0 );

statement.
 
D

Darío Griffo

ctest(ctest& c) { cout <<"ctest copy constr" << endl; };

You've forgot the const:
ctest(const ctest& c) { cout <<"ctest copy constr" << endl; };

Also up here

ctest& operator= (const ctest& a) { cout << "ctest copy assignment" <<

and hereoperator int()const { cout << "call operator int()" <<endl; return
20; }

I think it's a good question why the compiler thinks that a non const
reference should be a int..
All I did is gave you the practical solution, not the teorical.
I wonder where is the

return( 0 );

statement.

That's not necesary, it's not an error, but it's a good practice not
to forget all the pieces of a program :)
Darío
 
E

Elias Salomão Helou Neto

I wonder where is the

return( 0 );

statement.

Well, it does not quite solve the problem. In fact, the issue here is
const-correctness, because ctest() must be used as const, but ctest
has ctest& or int as arguments, being the second the only possible the
compiler tryes to find a conversion. The following does not compile:

#include <iostream>

class ctest {
public:
ctest(void) { std::cout << "ctest default constor" <<
std::endl; };
ctest(ctest& c) { std::cout <<"ctest copy constr" <<
std::endl; };
//ctest(int a) { std::cout <<"ctest int constor" <<
std::endl; };

ctest& operator= (ctest& a) { std::cout << "ctest copy
assignment" << std::endl; return *this; };
//operator int() { std::cout << "call operator int()"
<< std::endl; return 20; }

};

int main(void)
{
ctest cc = ctest();
return( 0 );
}

but the following does:

#include <iostream>

class ctest {
public:
ctest(void) { std::cout << "ctest default constor" <<
std::endl; };
ctest(const ctest& c) { std::cout <<"ctest copy
constr" << std::endl; };
ctest(int a) { std::cout <<"ctest int constor" <<
std::endl; };

ctest& operator= (ctest& a) { std::cout << "ctest copy
assignment" << std::endl; return *this; };
operator int() { std::cout << "call operator int()" <<
std::endl; return 20; }

};

int main(void)
{
ctest cc = ctest();
return( 0 );
}

and prints

ctest default constor

only once, because of optimization, I guess.
 
H

hill.liu

Well, it does not quite solve the problem. In fact, the issue here is
const-correctness, because ctest() must be used as const, but ctest
has ctest& or int as arguments, being the second the only possible the
compiler tryes to find a conversion. The following does not compile:

#include <iostream>

class ctest {
public:
ctest(void) { std::cout << "ctest default constor" <<
std::endl; };
ctest(ctest& c) { std::cout <<"ctest copy constr" <<
std::endl; };
//ctest(int a) { std::cout <<"ctest int constor" <<
std::endl; };

ctest& operator= (ctest& a) { std::cout << "ctest copy
assignment" << std::endl; return *this; };
//operator int() { std::cout << "call operator int()"
<< std::endl; return 20; }

};

int main(void)
{
ctest cc = ctest();
return( 0 );

}

but the following does:

#include <iostream>

class ctest {
public:
ctest(void) { std::cout << "ctest default constor" <<
std::endl; };
ctest(const ctest& c) { std::cout <<"ctest copy
constr" << std::endl; };
ctest(int a) { std::cout <<"ctest int constor" <<
std::endl; };

ctest& operator= (ctest& a) { std::cout << "ctest copy
assignment" << std::endl; return *this; };
operator int() { std::cout << "call operator int()" <<
std::endl; return 20; }

};

int main(void)
{
ctest cc = ctest();
return( 0 );

}

and prints

ctest default constor

only once, because of optimization, I guess.

Forget to mention, I use Microsoft C/C++ Optimizing Compiler Version
14.00.50727.42 without any optimization option.
I comment out ctest(int a) and operator int() it still compiles. And
the execution output is
"ctest default constor"

From your reply and my understanding right now, ctest cc = ctest() has
two part. First part is that ctest() will create a temp ctest object
using default constructor. And second part is to create object cc
using copy constructor with that temp ctest as argument. Because that
temp ctest is a const, and my original class definition has no const-
copy constructor; so compiler convert that temp ctest into int and use
ctest(int a) to create object cc.
But why it still compiles after comment out ctest(int a) and operator
int()?
And, after I add const to copy constructor like this: ctest(const
ctest& c) { cout <<"ctest copy constr" << endl; }, why it doesn't
output "ctest copy constr"?
 
K

Kai-Uwe Bux

Forget to mention, I use Microsoft C/C++ Optimizing Compiler Version
14.00.50727.42 without any optimization option.
I comment out ctest(int a) and operator int() it still compiles. And
the execution output is
"ctest default constor"

From your reply and my understanding right now, ctest cc = ctest() has
two part. First part is that ctest() will create a temp ctest object
using default constructor. And second part is to create object cc
using copy constructor with that temp ctest as argument. Because that
temp ctest is a const, and my original class definition has no const-
copy constructor; so compiler convert that temp ctest into int and use
ctest(int a) to create object cc.

That understanding is not entirely correct. The temporary

ctest()

is _not_ const. If ctest had a non-const member function foo(), you could do

ctest().foo()

and the compiler would not complain.

The misconception that temporaries are const stems from the provisions in
[8.5.3/5] that prohibit initializing a non-const reference from a
temporary. Your copy constructor takes its argument as a non-const
reference and since the compiler cannot use the temporary ctest() to
initialize it, it rejects this option.

But why it still compiles after comment out ctest(int a) and operator
int()?

It doesn't. You also have to change the copy constructor.
And, after I add const to copy constructor like this: ctest(const
ctest& c) { cout <<"ctest copy constr" << endl; }, why it doesn't
output "ctest copy constr"?

Because the compiler is allowed to optimize the copy constructor call away
even if that changes the observable behavior of the program.


Best

Kai-Uwe Bux
 
H

hill.liu

Forget to mention, I use Microsoft C/C++ Optimizing Compiler Version
14.00.50727.42 without any optimization option.
I comment out ctest(int a) and operator int() it still compiles. And
the execution output is
"ctest default constor"
From your reply and my understanding right now, ctest cc = ctest() has
two part. First part is that ctest() will create a temp ctest object
using default constructor. And second part is to create object cc
using copy constructor with that temp ctest as argument. Because that
temp ctest is a const, and my original class definition has no const-
copy constructor; so compiler convert that temp ctest into int and use
ctest(int a) to create object cc.

That understanding is not entirely correct. The temporary

ctest()

is _not_ const. If ctest had a non-const member function foo(), you could do

ctest().foo()

and the compiler would not complain.

The misconception that temporaries are const stems from the provisions in
[8.5.3/5] that prohibit initializing a non-const reference from a
temporary. Your copy constructor takes its argument as a non-const
reference and since the compiler cannot use the temporary ctest() to
initialize it, it rejects this option.
But why it still compiles after comment out ctest(int a) and operator
int()?

It doesn't. You also have to change the copy constructor.
And, after I add const to copy constructor like this: ctest(const
ctest& c) { cout <<"ctest copy constr" << endl; }, why it doesn't
output "ctest copy constr"?

Because the compiler is allowed to optimize the copy constructor call away
even if that changes the observable behavior of the program.

Best

Kai-Uwe Bux

Now I know why my original class definition will produce those
outputs.:)
But I refine class definition like Elias suggested:
class ctest {
public:
ctest(void) { std::cout << "ctest default constor" <<
std::endl; };
ctest(ctest& c) { std::cout <<"ctest copy constr" <<
std::endl; };

ctest& operator= (ctest& a) { std::cout << "ctest copy
assignment" << std::endl; return *this; };

};
I build this successfully under Visual Studio 2005 and "ctest cc =
ctest()" outputs "ctest default constor".
Why is that?
 
J

James Kanze

I stuck into this problem that I can't figure it out.
Here is the class definition:
class ctest {
public:
ctest(void) { cout << "ctest default constor" << endl; };
ctest(ctest& c) { cout <<"ctest copy constr" << endl; };
ctest(int a) { cout <<"ctest int constor" <<endl; };

ctest& operator= (ctest& a) { cout << "ctest copy assignment" <<
endl; return *this; };
operator int() { cout << "call operator int()" <<endl; return 20; }

};

int main(void)
{
ctest cc = ctest();

}

And it outputs as following:
ctest default constor
call operator int()
ctest int constor
I wonder why it will invoke "operator int()" and call "constructor
with int as argument".

Because that's what is required by the standard. Because the
source type is the same as the target type, the copy
initialization basically becomes the same as direct
initialization, e.g. as if you'd written

ctest cc( ctest() ) ;

The only constructor which can be called is the one taking an
int, so the compiler converts your expression into an int, using
the user defined conversion operator, and calls it.

Maybe you meant for the copy constructor (and the assignment
operator, while we're at it, although it isn't used in your
example) to take references to const, as is the normal case.
 
B

Bo Persson

On Jul 1, 11:49 am, Elias Salomão Helou Neto <[email protected]>
wrote:
On 1 jul, 00:24, Elias Salomão Helou Neto <[email protected]>
wrote:
On 1 jul, 00:16, "(e-mail address removed)" <[email protected]>
wrote:

I stuck into this problem that I can't figure it out.
Here is the class definition:
class ctest {
public:
ctest(void) { cout << "ctest default constor"
<< endl; }; ctest(ctest& c) { cout <<"ctest
copy constr" << endl; }; ctest(int a) { cout
<<"ctest int constor" <<endl; };
ctest& operator= (ctest& a) { cout << "ctest
copy assignment" <<
endl; return *this; };
operator int() { cout << "call operator int()"
<<endl; return 20; }

int main(void)
{
ctest cc = ctest();

And it outputs as following:
ctest default constor
call operator int()
ctest int constor
I wonder why it will invoke "operator int()" and call
"constructor with int as argument".

I wonder where is the
return( 0 );

Well, it does not quite solve the problem. In fact, the issue
here is const-correctness, because ctest() must be used as
const, but ctest has ctest& or int as arguments, being the
second the only possible the compiler tryes to find a
conversion. The following does not compile:
#include <iostream>
class ctest {
public:
ctest(void) { std::cout << "ctest default
constor" << std::endl; };
ctest(ctest& c) { std::cout <<"ctest copy
constr" << std::endl; };
//ctest(int a) { std::cout <<"ctest int constor"
<< std::endl; };
ctest& operator= (ctest& a) { std::cout <<
"ctest copy assignment" << std::endl; return *this; };
//operator int() { std::cout << "call operator
int()" << std::endl; return 20; }

int main(void)
{
ctest cc = ctest();
return( 0 );

but the following does:
#include <iostream>
class ctest {
public:
ctest(void) { std::cout << "ctest default
constor" << std::endl; };
ctest(const ctest& c) { std::cout <<"ctest copy
constr" << std::endl; };
ctest(int a) { std::cout <<"ctest int constor" <<
std::endl; };
ctest& operator= (ctest& a) { std::cout <<
"ctest copy assignment" << std::endl; return *this; };
operator int() { std::cout << "call operator
int()" << std::endl; return 20; }

int main(void)
{
ctest cc = ctest();
return( 0 );

and prints
ctest default constor
only once, because of optimization, I guess.
Forget to mention, I use Microsoft C/C++ Optimizing Compiler
Version
14.00.50727.42 without any optimization option.
I comment out ctest(int a) and operator int() it still compiles.
And the execution output is
"ctest default constor"
From your reply and my understanding right now, ctest cc =
ctest() has two part. First part is that ctest() will create a
temp ctest object using default constructor. And second part is
to create object cc using copy constructor with that temp ctest
as argument. Because that temp ctest is a const, and my original
class definition has no const- copy constructor; so compiler
convert that temp ctest into int and use ctest(int a) to create
object cc.

That understanding is not entirely correct. The temporary

ctest()

is _not_ const. If ctest had a non-const member function foo(),
you could do

ctest().foo()

and the compiler would not complain.

The misconception that temporaries are const stems from the
provisions in [8.5.3/5] that prohibit initializing a non-const
reference from a temporary. Your copy constructor takes its
argument as a non-const reference and since the compiler cannot
use the temporary ctest() to initialize it, it rejects this option.
But why it still compiles after comment out ctest(int a) and
operator int()?

It doesn't. You also have to change the copy constructor.
And, after I add const to copy constructor like this: ctest(const
ctest& c) { cout <<"ctest copy constr" << endl; }, why it doesn't
output "ctest copy constr"?

Because the compiler is allowed to optimize the copy constructor
call away even if that changes the observable behavior of the
program.

Best

Kai-Uwe Bux

Now I know why my original class definition will produce those
outputs.:)
But I refine class definition like Elias suggested:
class ctest {
public:
ctest(void) { std::cout << "ctest default constor"
<< std::endl; };
ctest(ctest& c) { std::cout <<"ctest copy constr" <<
std::endl; };

ctest& operator= (ctest& a) { std::cout << "ctest
copy assignment" << std::endl; return *this; };

};
I build this successfully under Visual Studio 2005 and "ctest cc =
ctest()" outputs "ctest default constor".
Why is that?

You are using the default settings for the compiler. You should select
"Disable Language Extensions (/Za)" to remove some of MS' extensions,
like letting the non-const copy constructor bind to a temporary. A
very nasty extension!


Bo Persson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top