Here is my code:
class Foo
{
public:
Foo(const char*);
};
class Bar
{
public:
Foo bu("adfds");
};
int main()
{
return 0;
}
Foo::Foo(const char* iLoc)
{ }
When I try to create a Foo class within the Bar class, I get the syntax error when trying to pass to the constructor. Why is this the case?
I prefer this to egrunin's answer as you don't have to track memory allocation.
class Bar
{
private:
Foo bu;
public:
Bar()
: bu("adfds")
{
}
};
You can't initialize bu in the class declaration. Is this what you want?
class Bar
{
public:
Foo *bu;
Bar() {
bu = new Foo("adfds");
}
};
Edit
As pointed out in the comments, here's a way of doing it without making bu a pointer:
class Bar : bu("adfds")
{
public:
Foo bu;
};
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 was going through a book called Programming Principles and Practices using C++ but found a strange behavior of class construction.
Suppose I have a class as follows:
class Foo {
public:
Foo(int x)
: y { x } { }
private:
int y;
};
and I have another class which has an instance of class Foo as its member object
class Bar {
public:
Bar(Foo x)
: y { x } { }
private:
Foo y;
};
When I do the following:
int main()
{
Bar obj_1 { Foo { 1 } };
Bar obj_2 { 2021 }; // this doesn't give me error?
return 0;
}
obj_1 was constructed as specified in the constructor, but obj_2 doesn't give me any error message and to me it seems it just magically works.
My intention of having a member of a class as an instance of another class was to force the constructor to take a class instance as its argument, but not an integer.
Why doesn't it give me incorrect type error?
You can prevent this implicit conversion by declaring the Foo constructor explicit
explicit Foo(int x) : y { x } { }
in main this would require the caller to change their obj_2 instantiation to
Bar obj_2 { Foo{2021} };
class foo{
public:
bar steal_the_moveable_object();
private:
bar moveable_object;
};
main(){
foo f;
auto moved_object= f.steal_the_moveable_object();
}
How can implement steal_the_movebale_object to move the moveable_object into the moved_object ?
You can simply move the member directly in the return statement :
class foo
{
public:
bar steal_the_moveable_object()
{
return std::move(moveable_object);
}
private:
bar moveable_object;
};
Beware that this may not be a good idea though. Consider using the following instead so that the method can only called on R-Values :
class foo
{
public:
bar steal_the_moveable_object() && // add '&&' here
{
return std::move(moveable_object);
}
private:
bar moveable_object;
};
int main()
{
foo f;
//auto x = f.steal_the_moveable_object(); // Compiler error
auto y = std::move(f).steal_the_moveable_object();
return 0;
}
my question is as follows: Suppose I have:
class Foo
{
public:
Foo() {}
void setInt(int i) { myInt = i; }
int getInt() { return myInt; }
private:
int myInt;
};
class Bar
{
public:
Bar(Foo f) { /* do something with f.getInt() */ }
};
Now I have another class that has Bar as a member vairable:
class BarUser
{
public:
BarUser();
private:
Bar bar;
};
I want to write BarUser's constructor, however I want to initialize Bar with a Foo member that has 3 as its integer. I.e.:
Foo f;
f.setInt(3);
Bar b(f);
However since I have Bar as a class member, I cannot write all this code in the initialization list... What I mean is:
BarUser::BarUser() : bar(/* Foo after executing f.setInt(3) */)
{ ... }
Suppose assignment operator is not allowed for Bar - how can I initialize it as intended?
Thanks!
If you can't change Foo, write a function:
Foo make_foo(int i) {
Foo f;
f.setInt(i);
return f;
}
then initialize with bar(make_foo(3)).
You've sort of shot yourself in the foot by giving Foo a constructor but no int constructor. You might be better off adding an explicit constructor to Foo that takes an int.
I have a base class:
class Foo {
public:
virtual ~Foo() {}
static void printFoos()
{
std::vector<Foo*>::iterator it;
for(it=fooList.begin();it!=fooList.end();++it)
{
std::cout<<"Class: "<<(*it)->getClassName()<<"\n";
}
}
virtual const char* getClassName()=0;
static std::vector<Foo*> fooList;
};
And a few derived classes, example given:
class Bar : public Foo {
public:
Bar();
private:
const char* getClassName()
{
return "Bar";
}
};
The above code gives "undefined reference to Foo::getClassName()" which Im assuming because the code wants to call Foo::getClassName(), but how do I get it to call the function like a virtual call normally? I.E. How do I get it to call Bar::getClassName() from inside Foo?
EDIT: Forgot inheritance thingy
Items in fooList must be created with new, fooList[0] = new Bar(). And Bar must inherit from Foo, as WeaselFox said.
There are two things that are undefined:
Bar::Bar() is undefined
- Bar();
+ Bar() {}
and fooList is undefined:
+std::vector<Foo*> Foo::fooList;
Here is corrected program:
test.cpp:
#include <vector>
#include <iostream>
class Foo {
public:
virtual ~Foo() {}
static void printFoos()
{
std::vector<Foo*>::iterator it;
for(it=fooList.begin();it!=fooList.end();++it)
{
std::cout<<"Class: "<<(*it)->getClassName()<<"\n";
}
}
virtual const char* getClassName()=0;
static std::vector<Foo*> fooList;
};
std::vector<Foo*> Foo::fooList;
class Bar : public Foo {
public:
Bar() {};
private:
const char* getClassName()
{
return "Bar";
}
};
int main()
{
//intentionally leaked
Foo::fooList.push_back(new Bar());
Foo::fooList.push_back(new Bar());
Foo::fooList.push_back(new Bar());
Foo::printFoos();
}
output:
Class: Bar
Class: Bar
Class: Bar
It seems bar does not inherit foo. You need to declare inheritance:
class bar: public foo { ...