Solved

problem with unions

Posted on 1998-05-19
5
912 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

Independent Software Vendors: 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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

734 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