473,395 Members | 1,595 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

simple constructor query

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
5 1293
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Vivek Shah | last post by:
Hi, Given below is apiece of code which I was writing to clear my concepts of copy constructor. I have a function f() which takes Class A object through call by value and return the same object....
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
3
by: Mike | last post by:
Hello In the following code,why are there 2 different Class1's.Is one a class definition and the other a function? namespace Music { public class Class1 { public Class1() {
12
by: satyajit | last post by:
I am trying to learn the concept of constructors in ECMAScript. I executed following code (See execution in Rhino JavaScript shell): function Foo(a) { this.a = a; } function Bar(b) { this.b...
8
by: Bern McCarty | last post by:
I have a simple ref class in its own namespace that needs to coexist with a legacy typedef alias for "unsigned int" in the global namespace that has the identifier as itself. Everything compiles...
4
by: DBC User | last post by:
I have 2 constructors in my class, first one with no paramter and the second one is with a paramter. I want the later constructor to use the first constructor and do some more with the parameter....
7
by: sachinc.biradar | last post by:
Hi, I am pretty new to C++, I have following doubt. Why can't we pass an argument to constructor as pointer? As per my knowledge referance is like const pointer. Any help will be highly...
14
by: Rob Wilkerson | last post by:
Hey all - Not being a seasoned PHP developer, tonight I started playing with the use of the Singleton pattern to store configuration information. What I was surprised to find was that the...
4
by: Arun Srinivasan | last post by:
Hi I was using a query previously, that was efficient select * from table where pred1 and pred2 and pred3; Later I was asked to introduce new ones, but they were not based on table columns but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.