Class-dilemma; One variable should contain all three classes

This is a discussion on Class-dilemma; One variable should contain all three classes within the C++ Programming forums, part of the General Programming Boards category; Lets say I have these three classes, I mean, lets just not say, but I really have these three classes ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Location
    Norway
    Posts
    25

    Class-dilemma; One variable should contain all three classes

    Lets say I have these three classes, I mean, lets just not say, but I really have these three classes and they cannot be changed;

    Code:
    class A
    {
    
    };
    
    class B : public A
    {
    
    };
    
    class C : public A
    {
    
    };
    How should I go about creating one variable which then will contain one object from each class?
    (I know I would have solved this by making C : public B... that is not an option)

    If I go:
    C class = new C(); the variable name "class" only contains C and A.
    If I do:
    B class = new B(); the variable name "class" only contains B and A.
    If I do:
    A class = new A(); the variable name "class" only contains A.

    Can I put two pointers in A, one that points to B and one that points to C? But that would again create two new instances of A? right?

    If I add a pointer in B to point to C (or vice versa) it will leave me with two A objects (one for the class, one for the pointer).

    Am I Clear?
    Ideas very much appreciated!

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,230
    Are you looking for virtual inheritance?

    Soma

  3. #3
    Registered User
    Join Date
    Mar 2010
    Location
    Norway
    Posts
    25
    Hmm... No idea what that is. *googling*

    I am sort of looking for that, I think... but, take this sample:
    Code:
    class A
    {
    private:
        int a;
    public:
        int geta()
        {
            a = 1;
            return a;
        }
    };
    
    class B : virtual public A
    {
    private:
        int b;
    public:
        int getb()
        {
            b = 2;
            return b;
        }
    };
    
    class C : virtual public A
    {
    private:
        int c;
    public:
        int getc()
        {
            c = 3;
            return c;
        }
    };
    
    class D : public C, public B
    {
    
    };
    
    int main()
    {
        D* d = new D();
        cout << d->geta();
        cout << d->getc();
        cout << d->getb();
    
    
        return 0;
    }
    But, I want to do the above code "snippet"; without the fourth class D...
    Is that even possible? No ? Not without letting B go to C or C go to B?

    Or I could add a pointer in either C or B, then create one object of the one with the pointer? hmm... trying!

    Code:
    #include <iostream>
    
    using namespace std;
    
    class A
    {
    private:
        int a;
    public:
        int geta()
        {
            a = 1;
            return a;
        }
    };
    
    class B : virtual public A
    {
    private:
        int b;
    public:
        int getb()
        {
            b = 2;
            return b;
        }
    };
    
    class C : virtual public A
    {
    private:
        int c;
        B* bpointer;
    public:
        C()
        {
            bpointer = new B();
        }
    
        void useB()
        {
            cout << bpointer->getb();
        }
        int getc()
        {
            c = 3;
            return c;
        }
    };
    
    int main()
    {
        C* d = new C();
        cout << d->geta();
        cout << d->getc();
        d->useB();
    
    
        return 0;
    }
    Both works the way I want to, but not the way I thought I could do it!
    It became so messy with a pointer instead of a fourth class. Damn!

    It would be cleaner if class A could contain both B and C... but thats not an option.
    Any other ideas ?
    Last edited by ManyTimes; 04-13-2010 at 05:04 PM.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    109
    What is the point of doing that? If you are not making a 4th class that inherits from B and C, then why even have two different classes? Essentially, you are trying to make B and C the same class.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Location
    Norway
    Posts
    25
    syzgy: I know, I could have put all three classes actually inside one class, but thats not the point. That did not answer your question; "What is the point of doing that?".

    Doing what? The last sample? Simply to have B and C still be a member of A, while A is only created once, same as the first sample, they do more or less the same thing, just that the more functions I add to B, they all have to be called from C..


    >>>"B and C the same class"
    Actually, all three classes could be in the one and same class, but having over 100 attributes in one class is quite rough?

  6. #6
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Sometimes the better path is to ask how to accomplish a particular goal than to ask how to follow a specific path. If you do the former and there is a better path to that goal then someone might volunteer a better path *to* that goal....the latter simply reinforces the travelling of a particular path, regardless if it is the right path or not...
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  7. #7
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Quote Originally Posted by jeffcobb View Post
    Sometimes the better path is to ask how to accomplish a particular goal than to ask how to follow a specific path. If you do the former and there is a better path to that goal then someone might volunteer a better path *to* that goal....the latter simply reinforces the travelling of a particular path, regardless if it is the right path or not...
    I second that. If you ask about how to solve a particular problem, maybe other people will tell you a way to do it that hasn't crossed your mind yet.
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  8. #8
    Registered User
    Join Date
    Mar 2010
    Location
    Norway
    Posts
    25
    Why do you guys always spam a thread with posts that are not topic related? Post count ftw? Do not answer!

    I have asked for "Any other ideas?", which mean if anyone has another way of doing this than what I've shown, if you haven't, or you do not want to share it for some odd reason, well do not post then.

    *Sometimes I really miss thehelper.net*

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by ManyTimes View Post
    ]
    I have asked for "Any other ideas?", which mean if anyone has another way of doing this than what I've shown, if you haven't, or you do not want to share it for some odd reason, well do not post then.

    *Sometimes I really miss thehelper.net*
    We're just wishing you would tell us what "this" is. (Hint: Your first post is not it. Your first post is a homework assignment made by a teacher who doesn't know programming, but likes logic puzzles, but not real logic puzzles, rather the kind of logic puzzles that depend on using a word in a non-standard way.) Tell us what you're trying to accomplish, and we can help. The only thing we know is that you seem to think you don't want 100 "attributes", whatever they are, in one class. (As a note, if I'm counting correctly, there are 150-odd member ... things ... (constructors, functions, types, etc.) in the std::string class. And if they all belong together, then why not put them all together?) And maybe you have to have some inheritance (since you said you had to have three classes), or maybe not (since you said you could get away with just one class).

    EDIT: Something more specific to help illustrate: you say that "everything I add to B will have to be called from C". So -- that implies that there should be some relationship between whatever B actually is and whatever C actually is; and that relationship will tell you how to code it (speaking in generalities). We don't know what that relationship is. We can guess until we get it right, but even we get tired of guessing after a while.
    Last edited by tabstop; 04-13-2010 at 09:08 PM.

  10. #10
    Registered User
    Join Date
    Apr 2010
    Posts
    10

    Answer to Querie

    C c=new C;
    now accessing all the variable of a&c
    and that of B can be accesed by B->x;(if x is any variable in it);
    So, only one instance is created;

    I hope you are satisfied if any more confusion informe me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class read as variable, default-type int? Say what?!
    By arcaine01 in forum C++ Programming
    Replies: 8
    Last Post: 07-10-2009, 05:34 PM
  2. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM
  3. About classes and HINSTANCE variable to Main
    By conright in forum Windows Programming
    Replies: 2
    Last Post: 01-01-2003, 07:00 PM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 01:26 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21