I'm attempting to do method chaining, however, instead of using the Methods in "Foo" I want to a constructor of a class (which is inherited from the base class):
class Bar {
public:
Bar() {
std::cout << "This is bar";
}
};
class Foo : public Bar {
public:
Foo() {
cout << "This is foo";
}
};
So my main would look like the following:
Foo f = Foo().Bar();
Why is this not possible in C++/C++11? Also, is there a way in which I can integrate this standard, or would I have to create an method in "Foo" which calls the constructor to "Bar"?
Edit:
class Bar {
public:
Bar() {
}
Bar& Options() {
cout << "sf";
return *this;
}
};
class Foo : public Bar {
public:
Foo() {
}
};
And then in main:
Foo F = Foo().Options();
Your updated question is illegal because Bar::Options() returns a reference to a Bar and you don't provide a way to convert a Bar to a Foo object.
Related
I am trying to use EXPECT_CALL to set the return value of the function that resides in a different class than what the mock class inherits from.
In the following snippet, I was wondering if there's a way to use EXPECT_CALL on someFunction() that's a public function of Bar inside a unit test through bar object of UnitTest class but it seems to error out.
One thing I know is I would need to use MOCK_METHOD for someFunction to override it to expect EXPECT_CALL but not sure how could I do inside the Mock class?
// Bar.hpp
class Bar
{
public:
virtual bool someFunction();
};
// Foo.hpp
namespace sw::foo_state
{
class Foo
{
Bar _bar;
public:
Foo(Bar&& bar) : _bar(std::move(bar)) {}
void TestFunction()
{
_bar.someFunction();
}
};
};
// MockClass.hpp
namespace sw
{
class Mock : public foo_state::Foo
{
Mock(Bar&& bar) : Foo(std::move(bar)) {}
};
};
// UnitTest.cpp
using namespace sw::foo_state;
class UnitTest
{
public:
Bar bar;
auto ptr = std::make_unique<Mock>(std::move(bar));
};
TEST_F(UnitTest, Test)
{
EXPECT_CALL(bar, someFunction()).WillOnce(Return(true)); // error: ‘class sw::foo_state::Bar’ has no member named ‘gmock_someFunction’; did you mean ‘someFunction’?
ptr->TestFunction();
}
EDIT:
You only need to change your class Foo constructor to accept a pointer or a reference to take advantage of class Bar polymorphism:
class Bar
{
public:
virtual bool someFunction()
{
return true;
}
};
class Foo
{
public:
Foo(Bar& bar): _bar{ bar} {}
void TestFunction()
{
std::cout << _bar.someFunction() << std::endl;
}
private:
Bar& _bar; // can also be std::unique_ptr<Bar> if you prefer to own the object
};
struct MockBar : public Bar
{
MOCK_METHOD0(someFunction, bool());
};
using testing::Return;
TEST(xxx, yyy)
{
MockBar mBar;
// ON_CALL(mBar, someFunction()).WillByDefault(Return(false)); // if you don't want to force expectation
EXPECT_CALL(mBar, someFunction()).WillOnce(Return(false));
Foo foo(mBar);
foo.TestFunction();
}
I've been trying to reference another class inside of a class in C++ and I have no idea how.
I have created a small program to demonstrate the issue
#include <iostream>
class foo{
public:
int variable1 = 012;
};
class bar{
public:
int getFooVariable(){
return variable1; // How would I get bar to refrence foo in foobar?
}
};
class foobar{
public:
bar p1;
foo p2;
};
int main(){
foobar fb;
std::cout << fb.p1.getFooVariable(); << std::endl;
return 0;
}
How would I solve this problem?
You're looking for dependency injection:
class foo
{
public:
int variable1 = 012;
};
class bar
{
foo _foo;
public:
bar(foo& fooInjected) :
_foo(fooInjected)
{}
int getFooVariable() { return _foo.variable1; }
};
There is an idea of inversion of control: bar has no control over the creation of foo instance, which is created outside. It's a common way to inject for instance a service. It's also a very important way to loosely couple classes working together and to mock and to test them. But here foo is a concrete class, instead and ideally you're refering only an interface (c#) or an abstract class (c++) in bar. Which concrete class is behind is out of control of bar. I recommend M. Seeman's book Dependency Injection in .NET to understand this completely.
or inheritance:
class foo
{
public:
int variable1 = 012;
};
class bar: public foo
{
public:
int getFooVariable() { return variable1; }
};
Here, bar builds up heavily on foo. bar is a "richer version" of foo if this makes sense. This is what to choose if there is a high cohesion inside bar.
Invoking a variable through another's instance function is a most horrid idea, that goes against class cohesion:
int getFooVariable(foo* foo) { return foo->variable1; }
Sorry to change the formatting, here's a solution:
#include iostream
class foo
{
public:
int variable1 = 012;
};
class bar
{
public:
int getFooVariable(foo* foo)
{
return foo->variable1; // How would I get bar to refrence foo in foobar?
}
};
class foobar
{
public:
bar p1;
foo p2;
};
int main()
{
foobar fb;
std::cout << fb.p1.getFooVariable(&(fb.p2));
<< std::endl;
return 0;
}
You can add a parameter to the getFooVariable() function to take a foo* reference. The reference can be found when you call the function.
My aim is to have a class that inherits from another class in C++ and overloads all of the parents class methods in an identical fashion.
So when a method is called some code is run, the original method is called and a bit more code is run all in the derived class overload method.
class Base
{
Base() {}
~Base() {}
void base_method()
{
// Does something.
}
}
template<class T>
class ClassWrapper : public T
{
public:
ClassWrapper(T base) : T( base ) {}
~ClassWrapper() {}
void wrap_function()
{
// multithread block {
// call base method within multithread block.
this->base_method();
// }
}
}
int main()
{
Base B;
ClassWrapper<Base> C( B );
C.base_method();
return 0;
}
Ideally nothing would be known about the base class but all of its methods could be overridden.
I'm not sure if this is even possible but if it is any suggestions would be great!
With inheritance, you might do:
class Base
{
Base() {}
virtual ~Base() {}
virtual void base_method()
{
// Does something.
}
};
class BaseWrapper : public Base
{
public:
BaseWrapper(Base base) : Bas( base ) {}
void base_method() override
{
// Some code ...
Base::base_method();
// Some code ...
}
}
int main()
{
Base B;
BaseWrapper C( B );
C.base_method();
}
Static polymorphism achieved through CRTP (Curiously Recurring Template Pattern) might be beneficial for you.
Read more about CRTP here and here.
Imagine you have a Wrapper class like:
template <typename Impl>
class Wrapper {
public:
Wrapper() {}
~Wrapper() {}
void some_preparation() {
std::cout << "Wrapper work!" << std::endl;
}
};
and then you have your actual class like:
class MyFoo : public Wrapper<MyFoo> {
public:
MyFoo() {}
~MyFoo() {}
void foo() {
Wrapper::some_preparation();
std::cout << "Derived work!" << std::endl;
}
};
and, eventually, you can use above code like:
MyFoo wrappedFoo;
wrappedFoo.foo();
The result would be:
Wrapper work!
Derived work!
Jarod's answer is a very good one for your question. However, I would like to add an answer more focused on your chosen design rather than the implementation.
Although you said that you want to "overloads all of the parents class methods in an identical fashion", your goal ("the original method is called and a bit more code is run all in the derived class overload method") indicates that it is slightly different.
The first one may indicate inheritance, but the second one may point to factory abstract design pattern (composition over inheritance):
#include<iostream>
class AbstractBar
{
public:
virtual void bar_method() = 0;
};
class Bar1 : public AbstractBar
{
public:
void bar_method() {
std::cout << "Bar 1" << std::endl;
}
};
class Bar2 : public AbstractBar
{
public:
void bar_method() {
std::cout << "Bar 2" << std::endl;
}
};
class Foo
{
public:
Foo(AbstractBar* bar_) : bar(bar_) { }
void foo_method() {
bar->bar_method();
std::cout << "Foo" << std::endl;
}
private:
AbstractBar* bar;
};
int main() {
Bar1 bar;
Foo foo(&bar);
foo.foo_method();
}
Being the output of the code:
Bar 1
Foo
Or a simplified version (based on your needs):
#include<iostream>
class Bar {
public:
void bar_method() {
std::cout << "Bar" << std::endl;
}
};
class Foo {
public:
Foo(Bar* bar_) : bar(bar_) { }
void foo_method() {
bar->bar_method();
std::cout << "Foo" << std::endl;
}
private:
Bar* bar;
};
int main() {
Bar bar;
Foo foo(&bar);
foo.foo_method();
}
Im relatively new to cpp. Im currently trying to write new testcases for a class foo,
Class foo2 {
Public:
Getsomevariable();
// do something
}
Class foo {
Private:
Foo2 foo2_obj;
}
Class testfoo {
protected:
Foo foo_obj;
}
TEST_F(testfoo, getsomevariabletest)
{ // ? How to access getsomevariable method of foo2 from foo_obj
Temp = foo_obj?
ASSERT_EQ(100,Temp);
}
Now Im testing using object of foo, but how should I be able to access foo2.Getsomevariable() from foo object?
Please help
I created a main to demonstrate the purposes of this, but tweak it for your own intended purpose.
class foo2 {
public:
std::string Getsomevariable() {
return "Hey!";
}
};
class foo {
public:
foo2 foo2_obj;
};
int main() {
foo f;
std::cout << f.foo2_obj.Getsomevariable();
}
class Foo
{
public:
void action();
};
class Bar : public Foo
{
public:
void action();
};
void Foo::action ()
{
cout << "parent\n";
};
void Bar::action ()
{
cout << "child\n";
};
int main()
{
Foo* foo = new Bar ();
foo->action(); // returns "parent" - "child" expected
return 1;
}
I'm sorry for a probably trivial question, but I'm new to C++...
The 'foo' pointer must point to an instance of Foo class, since it can be any of Foo's childs e.g. Bar, Bar1, Bar2, Bar3 etc.
And at the same time 'foo->action()' should run an overridden function of the child.
Tell me please, how do I correct the code to reach my goals...
Thanks!
Unlike other languages, like Java, in C++ base class have to specifically mark the methods it allows overriding of by using keyword virtual
The member function needs to be declared virtual (unlike Java for example, where all methods are implicitly virtual):
class Foo
{
public:
virtual void action();
};
Use keyword virtual for parent-class function. And also, your parent class should have virtual destructor.
class Foo
{
public:
virtual void action();
};
class Bar : public Foo
{
public:
void action();
};
void Foo::action ()
{
cout << "parent\n";
};
void Bar::action ()
{
cout << "child\n";
};
int main()
{
Foo* foo = new Bar ();
foo->action(); // returns "parent" - "child" expected
return 1;
}