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

Confusion on Virtual function,copy constructor,and type conversion

7
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class   A
  5. {
  6. public:
  7.     A() {cout << "constructor A" << endl;}
  8.     A(A& a) {cout<<"copy A"<<endl;}
  9.     virtual   void   Test(){cout <<"A   test" <<endl;}
  10. };
  11.  
  12. class   B:public   A
  13. {
  14. public:
  15.     B() {cout << "constructor B" << endl;}
  16.     B(B& b) {cout<<"copy B"<<endl;}
  17.     void   func(){Test();}
  18.     virtual   void   Test(){cout <<"B   test" <<endl;}
  19. };
  20.  
  21.  
  22. class   C:public   B
  23. {
  24. public:
  25.     C() {cout << "constructor C" << endl;}
  26.     C(C& c) {cout<<"copy C"<<endl;}
  27.     virtual void   Test(){cout <<"C   test" <<endl;}
  28. };
  29.  
  30.  
  31. void   main()
  32. {
  33.     C   c;
  34.     ((B*)(&c))-> func();
  35.     ((B)c).func();
  36.     cout.flush();
  37.  
  38.     int i;
  39.     cin >> i;
  40. }
  41.  
  42. //output is here:
  43.  
  44. constructor A
  45. constructor B
  46. constructor C
  47. C   test
  48. constructor A//Here is my question
  49. copy B
  50. B   test
I can't understand the above output, especially the latter part:
((B)c).func(); gives out the following results:
constructor A//Here is my question
copy B
B test

Please help me.Thanks!
Dec 17 '07 #1
1 1715
RRick
463 Expert 256MB
The problem is in line 35.
Expand|Select|Wrap|Line Numbers
  1. ((B)c).func();
You are telling the compiler to construct an object B from c. This will cause the compiler to make a copy of c using B's copy constructor which will first call the A constructor.

If you want a dynamic cast from C to B without the construction of a new object, you need to convert the pointer (or possibly the reference) to c. The code looks like
Expand|Select|Wrap|Line Numbers
  1. ((B*)(&c))->func();
.
Dec 17 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: ccs | last post by:
In Meyers' book he gave an example of "virtual copy constructor", which is quite different to an "ordinary" copy constructor by: 1. it returns a pointer to an object instead of a reference. 2. it...
1
by: Bob | last post by:
Hi, I'm trying to use a map with a string key, and a pointer to objects contained in a vector. I've wrapped this in a class like so: // cMap template<class T> class cMap : public cList<T> { ...
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...
16
by: plmanikandan | last post by:
Hi, I have doubts reg virtual constructor what is virtual constructor? Is c++ supports virtual constructor? Can anybody explain me about virtual constructor? Regards, Mani
11
by: Nindi73 | last post by:
A few days a ago I posted my code for a deep copy pointer which doesn't require the pointee object to have a virtual copy constructor. I need help with checking that it was exception safe and...
4
by: coder_lol | last post by:
Given the template class below, please help me understand the following code behavior? Array<int32ia(10); // Array that can contain 10 int32 ia = 1; // Array element 1 set to...
1
by: Bart Simpson | last post by:
Can anyone explain the concept of "slicing" with respect to the "virtual constructor" idiom as explain at parashift ? From parashift: class Shape { public: virtual ~Shape() { } ...
17
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;"...
11
by: subramanian100in | last post by:
Suppose we have a class named Test. Test obj; // assuming default ctor is available Test direct_init(obj); // direct initialization happens here Test copy_init = obj; // copy initialization...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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: 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: 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
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...

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.