473,382 Members | 1,362 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,382 software developers and data experts.

stupid question C++

I think I saw some erratic results with different (some outdated)
compilers using a conventional inheritence model.

class A {
//details omitted
A(){...}
A(const A&){...}
operator=(const A &){...}

};

class B:A{
B(){...}
B(const B&){...}
operator=(const B &){...}
};
B b1;
B b2(b2); will the A copy constructor or A default constructor be
called prior to B copy constructor?

if B copy constructor is declared B(const B&)A(){...}

will both default and copy constructor of A be called?

same for overloaded operator - Does A's version of overloaded operator
is called as well?

thx.

Jul 23 '05 #1
3 1062
Hi

puzzlecracker wrote:

[...]
B b1;
B b2(b2); will the A copy constructor or A default constructor be
called prior to B copy constructor?
I think you meant "B b2(b1);".
The default constructor, as you haven't specified anything else in the
initialization list.
if B copy constructor is declared B(const B&)A(){...}

will both default and copy constructor of A be called?
I think you meant "B(const B&) : A() {...}".
No, the behaviour will be the same as above, only A's default constructor
will be invoked.
same for overloaded operator - Does A's version of overloaded operator
is called as well?


For the implicitly defined assignment operator: yes
Otherwise: only if you explicitly call it.
Markus
Jul 23 '05 #2
puzzlecracker wrote:
I think I saw some erratic results with different (some outdated)
compilers using a conventional inheritence model.
Oh yeah? Can you explain specifically what happened? I'd like to know
because I will try avoid these compilers in the future. They seem to be
quite broken.
class A {
//details omitted
A(){...}
A(const A&){...}
operator=(const A &){...}
};

class B:A{
B(){...}
B(const B&){...}
operator=(const B &){...}
};
B b1;
B b2(b2);
This should be

B b2(b1);
will the A copy constructor or A default constructor be
called prior to B copy constructor?
Depends on what you write. If you write

B::B(const B &b)
{
}

the default constructor will be called and if you write

B::(const B &b)
: A(b)
{
}

the copy constructor will be called.
if B copy constructor is declared B(const B&)A(){...}
This should be

B::B(const B &b)
: A()
{
}
will both default and copy constructor of A be called?
Both? A constructor cannot be executed twice. In this case, the default
constructor will be called.
same for overloaded operator - Does A's version of overloaded operator
is called as well?


No, you have to do this manually:

B &B::operator=(const B &b)
{
A::operator=(b);

// B's assignment code
}
Jonathan

Jul 23 '05 #3
puzzlecracker wrote:
I think I saw some erratic results with different (some outdated)
compilers using a conventional inheritence model.

class A {
//details omitted
A(){...}
A(const A&){...}
operator=(const A &){...}

};

class B:A{
B(){...}
B(const B&){...}
operator=(const B &){...}
};
B b1;
B b2(b2); will the A copy constructor or A default constructor be
called prior to B copy constructor?

if B copy constructor is declared B(const B&)A(){...}

will both default and copy constructor of A be called?

same for overloaded operator - Does A's version of overloaded operator
is called as well?


What does your compiler(s) output when executing this code:
#include <iostream>
#include <ostream>

struct A
{
A()
{
std::cout << "A default\n";
}

A( const A & )
{
std::cout << "A copy\n";
}

A operator- ( const A & )
{
std::cout << "A operator -\n";
return * this;
}

};

struct B1 : A
{

B1()
{
std::cout << "B1 default\n";
}

B1( const B1 & )
{
std::cout << "B1 copy\n";
}

B1 operator- ( const B1 & )
{
std::cout << "B1 operator -\n";
return * this;
}

};

struct B2 : A
{

B2()
{
std::cout << "B2 default\n";
}

B2( const B2 & )
: A()
{
std::cout << "B2 copy\n";
}

B2 operator- ( const B2 & )
{
std::cout << "B2 operator -\n";
return * this;
}

};

struct B3 : A
{

B3()
{
std::cout << "B3 default\n";
}

B3( const B3 & b )
: A(b)
{
std::cout << "B3 copy\n";
}

B3 operator- ( const B3 & )
{
std::cout << "B3 operator -\n";
return * this;
}

};
template <typename X>
void f()
{

std::cout << "STEP a - construct\n";

X x;

std::cout << "STEP b - copy construct\n";

X x1( x );

std::cout << "STEP c - subtract\n";

x - x1;

std::cout << "DONE\n\n";
}

int main()
{
std::cout << "DO - A\n";
f<A>();

std::cout << "DO - B1\n";
f<B1>();

std::cout << "DO - B2\n";
f<B2>();

std::cout << "DO - B3\n";
f<B3>();
}
I believe the correct answer is this:

DO - A
STEP a - construct
A default
STEP b - copy construct
A copy
STEP c - subtract
A operator -
A copy
DONE

DO - B1
STEP a - construct
A default
B1 default
STEP b - copy construct
A default
B1 copy
STEP c - subtract
B1 operator -
A default
B1 copy
DONE

DO - B2
STEP a - construct
A default
B2 default
STEP b - copy construct
A default
B2 copy
STEP c - subtract
B2 operator -
A default
B2 copy
DONE

DO - B3
STEP a - construct
A default
B3 default
STEP b - copy construct
A copy
B3 copy
STEP c - subtract
B3 operator -
A copy
B3 copy
DONE
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: sebb | last post by:
I'm kind of newbie to programming, but I thought of something and I want some opinions on that. It's about a new instruction block to do some cycles. I thought about that because it's not very...
119
by: rhat | last post by:
I heard that beta 2 now makes ASP.NET xhtml compliant. Can anyone shed some light on what this will change and it will break stuff as converting HTML to XHTML pages DO break things. see,...
2
by: Lampa Dario | last post by:
Hi, where is this stupid error in this program? When I execute it, i receive a segmentation fault error. #include <stdio.h> int main(int argc, char *argv, char *env) { int i=0; int l=0; int...
10
by: Fei Li | last post by:
Hi, If I'm not stupid for this, then the design of FolderBrowserDialog is very stupid. It only allow to strat browing from several predefined folder(Environment.SpecialFolder). What can I do if...
5
by: Guoqi Zheng | last post by:
I think I have this kind of problem very often, now it is really making me crazy. VS.NET always want to change my html code. Ok, that is fine as long as Vs.Net can ask my confirmaton first. For...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.