By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,905 Members | 1,293 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,905 IT Pros & Developers. It's quick & easy.

simple constructor query

P: n/a
Hi All,

why does this

#include <iostream>
using namespace std;

class A {
public:
A (int = 0);
void assign (const A&);
private:
int number;
};

inline A::A(int n): number(n){
cout << "constructor\n";
}

inline void A::assign(const A& a){
number = a.number;
}

int main(){
A a(1);
a.assign(2);
return 0;
}

end up making two constructor calls? I thought the pass by reference would
have avoided the second call....is it beacuse the assign() call has a const
so a second copy must be made?

Thanks for your help

Regards

Michael
May 27 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On May 27, 11:01 am, "michael" <s...@begone.netwrote:
Hi All,

why does this

#include <iostream>
using namespace std;

class A {
public:
A (int = 0);
void assign (const A&);
private:
int number;

};

inline A::A(int n): number(n){
cout << "constructor\n";

}

inline void A::assign(const A& a){
number = a.number;

}

int main(){
A a(1);
a.assign(2);
return 0;

}

end up making two constructor calls? I thought the pass by reference would
have avoided the second call....is it beacuse the assign() call has a const
so a second copy must be made?

Thanks for your help

Regards

Michael
The statement "a.assign(2);" equals these two statement:
A temp(2);
a.assign(temp);
"A temp(2)" cause the second construcot call.

May 27 '07 #2

P: n/a
On May 26, 11:01 pm, "michael" <s...@begone.netwrote:
Hi All,

why does this

#include <iostream>
using namespace std;

class A {
public:
A (int = 0);
void assign (const A&);
private:
int number;

};

inline A::A(int n): number(n){
cout << "constructor\n";

}

inline void A::assign(const A& a){
number = a.number;

}

int main(){
A a(1);
a.assign(2);
return 0;

}

end up making two constructor calls? I thought the pass by reference would
have avoided the second call....is it beacuse the assign() call has a const
so a second copy must be made?

Thanks for your help

Regards

Michael
The second call is invoked on a temporary. Which would fail if A's
ctor was 'explicit'.
Instead of void A::assign(const A& a) you might consider a rudimentary
setter since class A will already have a compiler generated copy ctor
(assuming the compiler detects that one is needed). Which explains the
output below.

#include <iostream>

class A {
int number;
public:
A(int n = 0) : number(n) { }
void set(const int n) { number = n; }
int get() const { return number; }
};

int main()
{
A a;
a.set(99);
A another(a); // invokes copy
std::cout << another.get() << std::endl; // 99
}

___
That copy ctor looks something like this (look familiar?)

A::A(const A& copy)
{
number = copy.number;
}

May 27 '07 #3

P: n/a
michael wrote:
Hi All,

why does this

#include <iostream>
using namespace std;

class A {
public:
A (int = 0);
void assign (const A&);
private:
int number;
};

inline A::A(int n): number(n){
cout << "constructor\n";
}

inline void A::assign(const A& a){
number = a.number;
}

int main(){
A a(1);
a.assign(2);
return 0;
}

end up making two constructor calls? I thought the pass by reference would
have avoided the second call....
2 is not of type A, so it must first be converted to that type. That's the
second constructor call that you see.

May 27 '07 #4

P: n/a
On Sun, 27 May 2007 11:37:43 +0200, Rolf Magnus wrote:
>michael wrote:
>Hi All,

why does this

#include <iostream>
using namespace std;

class A {
public:
A (int = 0);
void assign (const A&);
private:
int number;
};

inline A::A(int n): number(n){
cout << "constructor\n";
}

inline void A::assign(const A& a){
number = a.number;
}

int main(){
A a(1);
a.assign(2);
return 0;
}

end up making two constructor calls? I thought the pass by reference would
have avoided the second call....

2 is not of type A, so it must first be converted to that type. That's the
second constructor call that you see.
Therefore add the following function to the class:

A& void A::assign(int i){
number = i;
return *this;
}
--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
May 27 '07 #5

P: n/a
On 27 May, 11:42, rpbg...@yahoo.com (Roland Pibinger) wrote:
On Sun, 27 May 2007 11:37:43 +0200, Rolf Magnus wrote:
michael wrote:
<snip>
inline void A::assign(const A& a){
number = a.number;
}
int main(){
A a(1);
a.assign(2);
return 0;
}
end up making two constructor calls? I thought the pass by reference would
have avoided the second call....
2 is not of type A, so it must first be converted to that type. That's the
second constructor call that you see.

Therefore add the following function to the class:

A& void A::assign(int i){
number = i;
return *this;

}
Either remove the void, or remove the A& and the return *this;

Gavin Deane

May 27 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.