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();
}
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);
}
/* ... */
Related
I want to create ab object of the base class Foo that initializes the variable value to something I decide, I then want to create an object of the derived class Bar and the variable value I decided earlier sticks with Foo that Bar inherits, like a constant one time initialization for the variable value.
Is there a way to do this without passing in the value every time I create the Bar object like with the Foo object or make the class value default to 5?
Example:
// Base class
class Foo {
private:
int value;
public:
Foo() {}
Foo(int value) : value(value) {}
int getValue() { return value; }
};
// Derived class
class Bar : public Foo {
private:
public:
Bar() {}
};
Foo foo(5);
foo.getValue() // 5
Bar bar;
bar.getValue() // this I also want to be 5 now
You can do this with static variables, either in class scope or method scope (with the latter having some differences, like lazy initialization, thread safe initialization, etc. Although, it does not make a big difference in your use case, but it is good to keep the latter in mind as an option). For example
#include <iostream>
using std::cout;
using std::endl;
// Base class
class Foo {
private:
static int value;
public:
Foo() {}
Foo(int value) {
Foo::value = value;
}
int getValue() { return value; }
};
int Foo::value = 0;
// Derived class
class Bar : public Foo {
private:
public:
Bar() {}
};
int main() {
Foo foo(5);
cout << foo.getValue() << endl;
Bar bar;
cout << bar.getValue() << endl;
}
I have just provided you with a solution you want. Keep in mind that this might not be the best way to achieve what you want. An object's construction parameters should ideally only affect the current object.
I have a parent class and a child class where the parent class uses one of the child classes in one of the parent's methods. In this method the parent uses the constructor and a method (that is a virtual method inherited from the parent) of the child, I saw another question similar to this on stackoeverflow but they did not use use methods and constructors of the child (one person mentioned the problem was easier because they only used variables of the child-class). I tried some basic class forwarding in the parent (putting class child; at the top) and that did not work.
This is the set up without any public private distinctions an attempt at address the required headers:
//foo.h
class Foo{
int x;
Foo(int i);
virtual void funA ();
void funB();
};
//foo.cpp
Foo::Foo(int i) {
x = i;
}
Foo::funA(){cout<<"Foo funA"<<endl;}
Foo::funB(){
Bar b = Bar();
b.funA();
}
//bar.h
class Bar : public Foo {
Bar(int i);
virtual void funA ();
};
//bar.cpp
Bar::Bar(int i) { x = i };
void Bar::funA(){cout<<"Bar funA"<<endl;}
I seem to be stuck on getting a the class forwarding to work. If someone could tell me how to set up my includes and class forwards that would be great!
Foo.h
class Foo {
int x;
Foo(int i);
virtual void funA();
void funB();
}
Foo.cpp
#include "Foo.h"
#include "Bar.h"
...
void Foo::funB() {
Bar b();
b.funA();
}
...
Bar.h has a dependency on Foo.h but you don't need any forward-declaration of classes (e.g. when you have a line like class Foo; that doesn't define a class body), just normal declarations. This is because Foo is not used by Bar's interface, only its implementation, so your Bar.h file can remain unchanged.
I have a class like this:
//class1.h
class Foo
{
private:
int m_Value;
public:
int Foo();
}
//class1.cpp
#include "class1.h"
#define CONST_VARIABLE 12
Foo::Foo()
{
m_Value = CONST_VARIABLE;
}
and a derived class:
//class2.h
#include "class1.h"
class Foo2 : public Foo
{
puplic:
Foo2();
//other functions here
}
//class2.cpp
#define CONST_VARIABLE 24;
#include "class2.h"
Foo2::Foo2() : Foo()
{
}
However, when I called
Foo a;
Foo2 b;
Those 2 (a and b) have the same m_Value (which is 12).
I knew Foo2() will call Foo() first so m_Value will take the CONST_VARIABLE in class Foo. Thus, I try re-define it but with no luck.
Since both classes are initialized the same way but with different default values and I can't add parameter to the default constructor.
How can I create a derived class with same default constructor but with different value? And in the long run, I could easily maintain or modify the code by changing the the value quickly.
You could use a protected constructor so that only derived classes can use the constructor specifying a different member value:
class Foo {
public:
Foo();
protected:
explicit Foo(int value);
private:
int m_Value;
};
Foo::Foo() :
m_Value(12)
{}
Foo::Foo(int value) :
m_Value(value)
{}
class Foo2 {
public:
Foo2();
};
Foo2::Foo2 :
Foo(24)
{}
Adhering to your requirement that there is only a default constructor, you can accomplish that with a template. Assuming you have full control over your implementation:
template <int CONST_VALUE>
class FooT {
protected:
int m_Value;
FooT () : m_Value(CONST_VALUE) {}
};
class Foo : FooT<12> {
//...
};
class Foo2 : FooT<24> {
//...
};
If Foo2 must inherit from Foo directly, you can move the parts that need to be initialized into a super parent class, and then use virtual inheritance of it by Foo so that Foo2 can initialize the super parent directly. This avoids needing to implement two constructors in Foo (one default one, and one to let Foo2 dictate initialization).
class FooBase {
protected:
int m_Value;
FooBase (int v) : m_Value(v) {}
};
class Foo : public virtual FooBase {
public:
Foo () : FooBase(12) {}
//...
};
class Foo2 : public Foo {
public:
Foo2 () : FooBase(24) {}
//...
};
How can I write code for a nested class' constructor? Because the following example gives me errors
foo.h
class foo
{
public:
class bar
{
public:
bar();
~bar();
}
private:
}
foo.cpp
#include "foo.h"
foo::bar()
{
}
You have to explicitly name the constructor as a member of foo::bar, not of foo. Adjust the name like this:
foo::bar::bar() {}
// ^^^^^
Since bar is a "derived class" of foo, which means if you want to access function bar(), you need to access the class bar first.
bar() is in the namespace of foo::bar::
You need to try foo::bar::bar()
class foo {
public:
foo() : _bar(this) {}
class bar {
public:
bar(const foo* base);
~bar() {}
private:
const foo* _base;
};
private:
bar _bar;
};
foo::bar::bar(const foo* base) : _base(base) {}
add in a variable in there to also show how you can call the outer class functions inside the nested class if need as well.
As a side not you also have error with your syntax. You are missing a ; on the classes closing }.
I am unsure about the order of declaration in my class. My compiler says error: "Foo" was not declared in this scope. If I change the order of public and private parts of the class, I end up with the same error. Also, if I want to use the getFoo(), and I am including my header file, the struct Foo type is not visible because it's private. But if I put it in public scope again, then public would have to come before private because otherwise the declaration of myFoo of type Foo can't happen since Foo was not decalred yet.
I am confused here... thanks for your help!
/*MyClass.h*/
class MyClass
{
private:
struct Foo{
int bar;
};
Foo myFoo;
public:
Foo getFoo(){ return myFoo;}
};
It has nothing to do with public or private. Your inner type has to be defined before it is used:
struct Foo
{
Bar bar() { return Bar(); } // ERROR
struct Bar {};
};
struct Foo
{
struct Bar {};
Bar bar() { return Bar(); } // OK
};
Note: there has been some confusion concerning the accessibility of a private type. THe type is accessible outside of its class, it just cannot be named. So, this code that accesses the private type is completely legal:
class Foo
{
struct Bar {
void do_stuff() const {}
};
public:
Bar bar() { return Bar(); } // OK
};
int main()
{
Foo f;
f.bar().do_stuff(); // use temporary Bar object
// In C++11, you can even make a named instance
auto b = f.bar(); // instantiate a Bar object called b
b.do_stuff(); // use it
}
Your struct Foo should be public otherwise getFoo getter will not work as Foo is accessible only inside your class