Solved

problem with unions

Posted on 1998-05-19
5
914 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

627 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