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

public virtual and assignment operators

Just ran into a problem upgrading from GCC 2.96 to GCC 3.3.2. There is a bug somewhere, could be in 2.96, could be in 3.3.2, could be in the code I'm compiling. Here's a minimal example

class A
{
public:
virtual int f() = 0;
private:
int a;
};

class B : public virtual A
{
};

class C: public virtual A
{
};

class D: public B, public C
{
public:
A* clone();
};

A*
D::clone()
{
return new D(*this);
}

The clone function uses the assignment operator (operator=) to return the new object.

The basis of this problem is that class A has data but no explicitly defined concrete functions.

The assignment operator in class D under both compilers correctly calls the assignment operators in classes B and C. Using GCC 2.96, assignment operators for both B and C call the assignment operator in A. While this does the work twice, at least it gets done.

Using 3.3.2, neither B nor C calls the assignment operator in A, so the new object contains uninitialized data.

Any idea which version of the compiler is correct (if either)? Should the assignment operator follow the same rules that constructors do, where the base class constructor is called first, and only once?

Thanks.
Sep 27 '06 #1
2 3064
Just ran into a problem upgrading from GCC 2.96 to GCC 3.3.2. There is a bug somewhere, could be in 2.96, could be in 3.3.2, could be in the code I'm compiling. Here's a minimal example

class A
{
public:
virtual int f() = 0;
private:
int a;
};

class B : public virtual A
{
};

class C: public virtual A
{
};

class D: public B, public C
{
public:
A* clone();
};

A*
D::clone()
{
return new D(*this);
}

The clone function uses the assignment operator (operator=) to return the new object.

The basis of this problem is that class A has data but no explicitly defined concrete functions.

The assignment operator in class D under both compilers correctly calls the assignment operators in classes B and C. Using GCC 2.96, assignment operators for both B and C call the assignment operator in A. While this does the work twice, at least it gets done.

Using 3.3.2, neither B nor C calls the assignment operator in A, so the new object contains uninitialized data.

Any idea which version of the compiler is correct (if either)? Should the assignment operator follow the same rules that constructors do, where the base class constructor is called first, and only once?

Thanks.
I found a solution to this problem. I created an assignment operator for D, as follows:

D& D:: operator=(const D& d)
{
if (this == &d)
return;

A:: operator=(d);
B:: operator=(d);
C:: operator=(d)
return *this;
}

This emulates the order in which constructors are run when classes B and C user public virtual derivation.
Sep 27 '06 #2
tyreld
144 100+
Actually, the problem is that you shouldn't even be allowed to allocate on object of class D. Class A has a pure virtual function f() therefore making it an abstract class. Any classes that inherit this function, but don't define it remain abstract. I imagine older versions of gcc allowed you to shoot youself in the foot this way. I would expect that from 2.96, but not from 3.3.2. Here is the error you should get at compile time (using gcc 4.1.0):

Expand|Select|Wrap|Line Numbers
  1. virtual.cpp: In member function ‘A* D::clone()’:
  2. virtual.cpp:26: error: cannot allocate an object of abstract type ‘D’
  3. virtual.cpp:18: note:   because the following virtual functions are pure within ‘D’:
  4. virtual.cpp:4: note:     virtual int A::f()
  5.  
Sep 27 '06 #3

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

Similar topics

19
by: qazmlp | last post by:
class base { // other members public: virtual ~base() { } virtual void virtualMethod1()=0 ; virtual void virtualMethod2()=0 ; virtual void virtualMethod3()=0 ;
2
by: Havatcha | last post by:
Does anyone know the syntax for accessing assignment operators in a Parent Class from a Child Class? If so, would be great if you could post a brief example here. Thanks.
17
by: N4M | last post by:
Dear, Suppose I have a Protocol class, in which I need also an assignment operator =(). class B { ..... virtual B& operator=(const B& rb) =0; }; Now in some derived class D: public B, how...
9
by: Ken Varn | last post by:
Is there anyway to override a public virtual method or property so that it is private in my derived class? I tried using new on the property and making it private, but no luck. --...
7
by: Calum | last post by:
Hi, I have a base class called Number that has subclasses Integer and Float. I want to be able to provide a virtual operator- in the base class so that I can subtract one Integer from another...
25
by: tsaar2003 | last post by:
Hi Pythonians, To begin with I'd like to apologize that I am not very experienced Python programmer so please forgive me if the following text does not make any sense. I have been missing...
5
by: David Delony | last post by:
I've hacked on shell and Perl and now I want to get seriously into C. I got bogged down in K & R, but I realized that I need to know more about the low-level computing stuff. I'd like some pointers...
2
by: sven.bauer | last post by:
Hi, I have a question following up the following slightly older posting: http://groups.google.de/group/comp.lang.c++/browse_thread/thread/40e52371e89806ae/52a3a6551f84d38b class Base {...
6
by: Jeff Bender | last post by:
I am trying to remember how to code in C++ after many years of using Java exclusively. I have this setup: class Base { public: virtual void printA(){} };
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.