Solved

problem with unions

Posted on 1998-05-19
5
911 Views
Last Modified: 2012-08-14
I'm working on a small C++ programming project in VC++ 5 and I'm running into a little trouble. Within a class I'm defining a union, and its members are instantiated classes. Example:

class clsA
{ ...
};

class clsB
{ ...
};

class clsC
{  union Foo
   {  clsA a;
      clsB b;
   } MyFoo;
};

At compile time, I recieve these errors:
error C2621: union 'Foo' : member 'a' has copy constructor
error C2621: union 'Foo' : member 'b' has copy constructor

Any ideas on how to resolve this problem?
0
Comment
Question by:mnichols
  • 3
5 Comments
 
LVL 1

Expert Comment

by:Nevering
ID: 1164360
can i see your actual code, i did the following, and no errors

class clsA
{
      int a;
      int b;
};

class clsB
{
      int c;
      int d;
};

class CImproView : public CScrollView
{

.... some stuff

      union foo
      {
            clsA a;
            clsB b;
      } myfoo;

.... some more stuff

};
0
 
LVL 22

Accepted Solution

by:
nietod earned 70 total points
ID: 1164361
answer coming.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1164362
A type (class) that has either a

constructor
destructor
copy constructor

may not be used within a union.  This because only one item of all the items in the union should be initialized (constructed), destroyed (destructor) or copied (copy constructor).  However the compiler does not know which item to use, so it cannot support types with these functions in unions.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1164363
As to how to resolve the problem.... that depends.  What are you trying to do?
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1164364
Store a pointer to an object in your union rather than the object itself.

class clsC {
  union Foo {
    clsA* pa;
    clsB* pb;
  } MyFoo;
};

BTW: You don't need 'MyFoo' - you may not need Foo or MyFoo above .. instead just have

class clsC {
  union {
    clsA* pa;
    clsB* pb;
  };
};

Alterntively, you may really be better with a tree of classes with a common subclass.

eg.
class Foo {
 ...
};

class clsA : public Foo {
  ...
};

class clsB : public Foo {
  ...
};

class clsC
  MyFoo* pFoo;
  ...
};

now your pFoo can point to either a clsA or clsB .. and you can use virtual functions and/or RTTI.

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question