-3

I am developing a class that has two constructors, that will use different objects. If I instance foo with no argument I want to use object bar, if I instance by foo(val) I want to use object baz.

I am getting confused how should I implement the solution. Inheritance does not seem so logical because there are differences on the functions and variables of bar and baz.

Below I present how I was thinking, basically I was setting a variable according to the called constructor. Then on my printWrapper, I was comparing this variable to know if I call bar print or baz print. One of the errors, it is due to the fact that bar and baz only have constructors with arguments. Also what I want is when I instance foo with/without argument I do not want object bar/baz created. How would you do it?

Header foo.h

    class foo {

    public:
           foo();
           foo(int val)

    private:
           bool object_bar;
           void printWrapper();
           bar b; // instance used on constructor foo()
           baz c; // instance used on constructor foo(val)
    }

Header bar.h

    class bar {

    public:
           bar(int val)

    private:
           bool test1;
           int test2;
           void print();
    }

Header baz.h

    class baz {

    public:
           baz(int val)

    private:
           bool test1;
           int test2;
           int test3;
           void print();
    }

Source foo.cpp

    foo::foo() : b(5) {
           this->object_bar = true;
    }

    foo::foo(int val) : c(val) {
           this->object_bar = false;
    }

    foo::printWrapper() {
           if (true == this->object_bar)
              b.print();
           else
              c.print();
    }
FAM
  • 23
  • 2
  • 1
    Why can't `foo` be a base class, and `baz_like_foo` and `bar_like_foo` be child classes of `foo`? `printWrapper` in `foo` then becomes a pure virtual function. – Bathsheba Apr 05 '18 at 16:57
  • 1
    `this->object_b = true;` shuld this be object_b**ar**? – Killzone Kid Apr 05 '18 at 17:01
  • Let's say bar and baz are two different communication protocols, so I use them on another classes. class foo can communicate use the two protocols. – FAM Apr 05 '18 at 17:04
  • `Class C` did you mean `class baz`? Also in c++ the keyword `class` starts with a lower case letter – Killzone Kid Apr 05 '18 at 17:06
  • Maybe what you want is `std::variant` ? (And for your `foo::printWrapper()`, look at `std::visit()`.) – Daniel Schepler Apr 05 '18 at 17:19
  • ***If*** `Bar` and `Baz` was two different communications protocols, then having each of the classes implement a common interface would be the usual solution. This is done through abstract base classes, virtual functions and inheritance. – Some programmer dude Apr 06 '18 at 02:32
  • To reiterate, this *really* screams out for virtual functions, inheritance and polymorphism in general. I suggest you get a couple of [good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) and read more about that. – Some programmer dude Apr 07 '18 at 08:08

1 Answers1

0

You can use a pointer to a base abstract class as a private member of your class Foo.

And then make class Bar and Baz inherits from this base class.

Then in your class foo :

/* declaration of the base class in foo.h */
class Base;

class Foo 
{
   /* ... */
   private:
        Base *m_base;
 };

and in foo.cpp :

#include "base.h"
#include "bar.h"
#include "baz.h"

Foo::Foo() {
       m_base = new Bar(5);
}

Foo::Foo(int val) {
       m_base = new Baz(val);
}
/* ... */
Fryz
  • 2,119
  • 2
  • 25
  • 45
  • 2
    Please consider using `std::unique_ptr` or some other safe alternative to `new`-`delete`. – patatahooligan Apr 05 '18 at 17:13
  • Yes you can of course handle memory management in various ways but i think the question is more related to class design here. – Fryz Apr 05 '18 at 17:15
  • Inevitably some of the users facing this problem and reading this answer will be beginners. The cleaner the example you provide, the more helpful it is to the community. – patatahooligan Apr 05 '18 at 17:19
  • Instead of constructing the object in the constructor it's also mostly preferable to pass an already constructed object. – Daniel Jour Apr 05 '18 at 17:37