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

explicit copy constructor

Hi, folks,

I am trying to make my copy constructor explicit, but when the
scenario below comes into being, weird things happen.
Here is the code snippet:
#include <iostream>
using namespace std;

class Base {
public:
Base() {
}
Base(const Base& other) {
}
};

template<typename T>
class Derived: public Base {
public:

Derived() {
}

template<typename U>
Derived(const Derived<U>& other): Base(*(Base*)&other) {
cout << "Cast copy constructor\n";
}

//here is the trouble maker
explicit Derived(const Derived& other): Base(*(Base*)&other) {
cout << "Normal copy constructor\n";
}
};

int main() {

Derived<intd1;

Derived<intd2 = d1;

return 0;
}

Intuitively, I want "Normal copy constructor" printed, but what I saw
is "Cast copy constructor", while I remove "explicit" keyword,
everything is OK, the output is "Normal copy constructor", any
informative tips will receive my big thanks.

Aug 10 '07 #1
1 2433
On 8 10 , 11 53 , "Alf P. Steinbach" <al...@start.nowrote:
An explicit constructor must be called explictly.
You don't call it explicitly, so how can you expect it to be called?
So, you mean if I define a explicit copy constructor, there can not be
any temporary object in my code, right?

Apart from that, first, you really don't want to use reinterpret_cast as
you do above, and secondly, the templated constructor isn't a copy
constructor -- a "copy constructor" is by definition not templated.
The templated constructor is used for type casting from Derived<Uto
Derived<T>.

Thanks for your informative tips.
Big thanks goes to you.

Aug 10 '07 #2

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

Similar topics

2
by: Dario | last post by:
Trying to compile the following code-fragment with g++ 2.96: class Entity { private: void * data; public: explicit Entity(int); explicit Entity(Entity &); virtual ~Entity(); void...
4
by: Jean Stax | last post by:
Hi ! I tried to understand when the explicit attribute in copy constructor prevents from me to create a new object. Bellow is the sample code. While the two first cases really generate a...
6
by: Christoph Bartoschek | last post by:
Hi, gcc 3.4 rejects the following program: class T { public: T() : a(3) {} explicit T(T const & other) : a(other.a) {} private: int a;
12
by: Marcelo Pinto | last post by:
Hi all, In practice, what is the diference between a default constructor and an explicit default constructor? class Ai { public: Ai() {} };
2
by: Dave | last post by:
Hello NG, Can anybody fathom the purpose of an explicit copy constructor? On page 232 of the Josuttis STL reference, I see a reference to such. How could you ever need to supress the...
1
by: Greg | last post by:
Referring to Managed: why do templates require an explicit copy constructor to be defined in order to use gcnew whereas simply declaring an object as a local (still uses managed heap of course)...
17
by: qazmlp1209 | last post by:
Will the following program cause any problem, because of the missing explicit copy constructor for the 'test' class? #include <vector> #include <iostream> class test { char* cPtr ;
19
by: Jeroen | last post by:
Hi guys, I have a simple question. If I have a class like: class A { A(); ~A(); A(A& a); A(int i);
1
by: denis | last post by:
Hello, I noticed the following in the "C++ Coding Standards" book. Why should the code on line 30 be in error? Transmogrify accepts a (B obj), and we are passing it a B obj. I understand why...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
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
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: 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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.