I have these 2 specific classes:
class foo{
private:
int a;
int b;
public:
foo(int x, int y)
:a(x), b(y)
{
cout << "I just created a foo! << endl;
}
~foo()
{
cout << "A foo was just destroyed!" << endl;
}
void set_a(int a_num)
{
a = a_num;
}
void set_b(int b_num)
{
b = b_num;
}
class bar{
private:
int T;
int S;
foo f;
public:
bar(int x, int y, foo n=(0,0) <--
:T(x), S(y), f(n)
{
cout << "I just created a f!" << endl;
foo.set_a(T); <--
foo.set_b(S); <--
}
~bar(){
cout << "A bar was destroyed!" << endl;
}
When a bar is created, i want the given values T and S to be assigned immediately to the foo object-member.At the marked lines i tried to overwrite these values but none of these seems to work and i get the error: "default argument for parameter of type foo has type 'int'".How can i get this to work?
You can use:
bar(int x, int y, foo n=foo(0,0)) : ... { ... }
If you are able to use a C++11 compiler, you can also use:
bar(int x, int y, foo n=foo{0,0}) : ... { ... }
and
bar(int x, int y, foo n={0,0}) : ... { ... }
Instead of:
foo.set_a(T);
foo.set_b(S);
you need to use:
f.set_a(T);
f.set_b(S);
since you need to call set_a and set_b on the member variable f.
A better alternative is to initialize f using:
bar(int x, int y} : T(x), S(y), f(x, y) {}
and leave the body of the constructor empty.
Related
I can't figure out why my output start with "ACCB.." - my expectation was "ACB.."
Why is the copy constructor of class A called twice?
#include <iostream>
using namespace std;
class A
{
int x, y, z;
public:
A(int x, int y, int z) :x(x), y(y), z(z) { cout << "A"; }
A(A& a) :x(a.x), y(a.y), z(a.z) { cout << "C"; }
void sh() { cout << x << y << z; }
};
class B
{
A a;
int q, r;
public:
B(A x, int y, int z) : a(x), q(y), r(z) { cout << "B"; }
void sh() { a.sh(); cout << q << r; }
};
int main()
{
A i(9, 7, 4);
B b(i, 3, 7);
b.sh();
}
Your copy constructor is being called 2 times because you are making 2 copies. 1 is being made implicitly by the compiler, and 1 is being made explicitly in your own code.
In your B constructor, its x parameter is being passed by value. That is an implicit copy being made when i in main() is assigned to x. Then, an explicit copy is made when x is passed to the copy constructor of the a member of B.
You can eliminate the implicit copy by changing the x parameter to be passed by reference instead.
Also, your A copy constructor should be declared as A(const A& a).
#include <iostream>
using namespace std;
class A
{
int x, y, z;
public:
A(int x, int y, int z) :x(x), y(y), z(z) { cout << "A"; }
A(const A& a) :x(a.x), y(a.y), z(a.z) { cout << "C"; }
void sh() { cout << x << y << z; }
};
class B {
A a;
int q, r;
public:
B(const A& x, int y, int z) : a(x), q(y), r(z) { cout << "B"; }
void sh() { a.sh(); cout << q << r; }
};
int main()
{
A i(9, 7, 4);
B b(i, 3, 7);
b.sh();
}
Why copy constructor of class A is called twice?
Because your B constructor takes its first argument (an object of class A) by value - so a copy of i is made, which is then passed to the constructor.
To prevent this, declare the first argument to the B constructor as a reference (as in the A copy constructor itself):
B(A& x, int y, int z) : a(x), q(y), r(z)
{
cout << "B";
}
I'd like to call a few methods of classes 'A' and 'B' from the class 'Caller'. I need to use a function pointer because I want to call different methods.
My method gets called, but when I try to access a member variable from it, my program crashes ('program.exe has stopped working').
How come that happens?
#include <iostream>
using namespace std;
template <class T>
class Caller
{
typedef void (T::*myFunc)(int);
public:
Caller(T* obj, myFunc fp)
{
f = fp;
}
void invoke(int foobar)
{
(o->*f)(foobar);
}
private:
myFunc f;
T* o;
};
class A
{
public:
A() : n(0) {}
void foo(int bar)
{
cout << "A::foo called (bar = " << bar << ", n = " << n << ")" << endl; // the crash occurs here, and 'this' equals 0 at this point
}
void setNum(int num)
{
n = num;
}
private:
int n;
};
class B
{
public:
B() : n(0) {}
void fooo(int bar)
{
cout << "B::fooo called (bar = " << bar << ", n = " << n << ")" << endl; // same here if I call B::fooo first
}
void setNum(int num)
{
n = num;
}
private:
int n;
};
int main()
{
A myA;
B myB;
myA.setNum(128);
myB.setNum(256);
Caller<A> cA(&myA, &A::foo);
Caller<B> cB(&myB, &B::fooo);
cA.invoke(10);
cB.invoke(20);
return 0;
}
Thank you in advance.
EDIT : I use VS2017 and I can build my program without getting any compiler errors.
My method gets called, but when I try to access a member variable from it, my program crashes ...
Because you forgot to assign passed obj to o pointer in your Caller:
template <class T>
class Caller
{
typedef void (T::*myFunc)(int);
public:
Caller(T* obj, myFunc fp)
{
o = obj; // << == you need this!
f = fp;
}
void invoke(int foobar)
{
(o->*f)(foobar);
}
private:
myFunc f;
T* o;
};
Also, in general it's better to use member initializer lists:
Caller::Caller(T* obj, myFunc fp) : o(obj), f(fp)
{
}
I am writing code to access private members of a class through another friend class. The below code works
// Example program
#include <iostream>
#include <string>
using namespace std;
class Foo
{
private:
int a;
protected:
public:
friend class Bar;
Foo(int x)
{
a = x ;
}
};
class Bar
{
private:
protected:
public:
int b;
Bar(Foo& f)
{
b = f.a;
cout << "f.a is " << f.a << endl;
}
};
int main()
{
Foo foo(5);
Bar bar(foo);
cout << "Value of variable b is " << bar.b << endl;
}
Above code works fine. However, if I want to access a private variable of Foo through a function in friend class Bar, I am unable to. See code below
#include <iostream>
#include <string>
using namespace std;
class Foo
{
private:
int a;
protected:
public:
friend class Bar;
Foo(int x)
{
a = x ;
}
};
class Bar
{
private:
protected:
public:
int b;
Bar(Foo& f)
{
b = f.a;
}
void printvariable(void)
{
cout << "f.a is " << f.a << endl;
}
};
int main()
{
Foo foo(5);
Bar bar(foo);
cout << "Value of variable b is " << bar.b << endl;
}
I totally understand why execution fails on the
void printvariable(void)
{
cout << "f.a is " << f.a << endl;
}
function since f is not in scope for the function. However, since I am passing Foo f in the constructor for Bar b, I am hoping to write code that will allow me to access members in Foo without passing Foo f to the function printvariable() again.
What is the most efficient way to write this code?
You can keep the reference to f. The code should be:
class Bar
{
private:
protected:
public:
int b;
Foo& f_ref;
Bar(Foo& f)
:f_ref(f)
{
b = f.a;
}
void printvariable(void)
{
cout << "f.a is " << f_ref.a << endl;
}
};
TEST!
You can do it like this, but if I were you I'd write some getters, also – class friendship isn't really recommended.
class Bar {
public:
Foo& ref;
Bar(Foo& f)
: ref { f }
{ }
void printvariable() {
cout << "f.a is " << ref.a << endl;
}
};
Btw there's no reason to add void in brackets in C++, it lost its meaning from C and has no effect by now.
You are wrong in one point. You are indeed passing a reference to f in the ctor, but the constructor and whole class Bar does not remember a whole object of f. In your original code, the constructor only makes the Bar object remember the int a part of the object, so only that little bit is later accessible:
class Foo
{
...
friend class Bar;
...
};
class Bar
{
...
int b;
Bar(Foo& f)
{
b = f.a; // <=--- HERE
}
void printvariable(void)
{
cout << "f.a is " << b << endl; // <-- now it refers B
}
Please note how your ctor of Bar only reads f.a and stores it in b. From now on, the Bar object only remembers b and that's all. You can freely access the b in printvariable. However, it will not be the a-taken-from-f. It will be b, that was set to the same value as f.a during constructor. Since that point of time, b and f.a are totally separate. That's how value copying works.
To make Bar remember whole f, you have to, well, remember whole f:
class Bar
{
...
Foo wholeThing;
Bar(Foo& f)
{
wholeThing = f; // <=--- HERE
}
void printvariable(void)
{
cout << "f.a is " << wholeThing.a << endl;
}
However, again, there's a catch: now since wholeThing is of type Foo, the constructor will actually make a copy of that object during wholeThing=f. Just the same as it was when b=f.a, but now it remembers a copy of whole f.
Of course, it's only matter of type. You can store a reference instead of whole-Foo, but it needs a bit different initialization syntax:
class Bar
{
...
Foo& wholeThing;
Bar(Foo& f) :
wholeThing(f) // <=--- HERE
{
// <=--- empty
}
void printvariable(void)
{
cout << "f.a is " << wholeThing.a << endl;
}
I'll describe my question using the following sample code.
I have class B defined as follows:
class B
{
public:
inline B(){}
inline B(int(*f)(int)) :myfunc{ f }{}
void setfunction(int (*f)(int x)) { myfunc = f; }
void print(int number) { std::cout << myfunc(number) << std::endl; }
private:
int(*myfunc)(int);
};
I then define class A as follows:
class A
{
public:
A(int myint) :a{ myint }{ b.setfunction(g); }
int g(int) { return a; }
void print() { b.print(a); }
private:
B b;
int a;
};
To me the issue seems to be that the member function g has the signature int A::g(int) rather than int g(int).
Is there a standard way to make the above work? I guess this is quite a general setup, in that we have a class (class B) that contains some sort of member functions that perform some operations, and we have a class (class A) that needs to use a particular member function of class B -- so is it that my design is wrong, and if so whats the best way to express this idea?
You can use std::function:
class B
{
public:
inline B() {}
inline B(std::function<int(int)> f) : myfunc{ f } {}
void setfunction(std::function<int(int)> f) { myfunc = f; }
void print(int number) { std::cout << myfunc(number) << std::endl; }
private:
std::function<int(int)> myfunc;
};
class A
{
public:
A(int myint) :a{ myint } {
b.setfunction([this](int a) {
return g(a);
}
);
}
int g(int) { return a; }
void print() { b.print(a); }
private:
B b;
int a;
};
You could generalize the class B. Instead of keeping a pointer (int(*)(int)), what you really want is any thing that I can call with an int and get back another int. C++11 introduced a type-erased function objection for exactly this reason: std::function<int(int)>:
class B
{
using F = std::function<int(int)>
public:
B(){}
B(F f) : myfunc(std::move(f)) { }
void setfunction(F f) { myfunc = std::move(f); }
void print(int number) { std::cout << myfunc(number) << std::endl; }
private:
F myfunc;
};
And then you can just provide a general callable into B from A:
A(int myint)
: b([this](int a){ return g(a); })
, a{ myint }
{ }
Use std::function and std::bind
class B
{
public:
inline B(int(*f)(int)) :myfunc{ f }{}
void setfunction(std::function<int(int)> f) { myfunc = f; }
void print(int number) { std::cout << myfunc(number) << std::endl; }
private:
std::function<int(int)> myfunc;
};
// ...
A a;
B b(std::bind(&A::g, &a));
Also note that you should initialize the function pointer to some default value (most likely null) and check for it when using, otherwise it's value is undefined.
You could use std::bind to bind the member function A::g.
class B
{
public:
inline B(){}
inline B(std::function<int(int)> f) :myfunc{ f }{}
void setfunction(std::function<int(int)> f) { myfunc = f; }
void print(int number) { std::cout << myfunc(number) << std::endl; }
private:
std::function<int(int)> myfunc;
};
class A
{
public:
A(int myint) :a{ myint } {
b.setfunction(std::bind(&A::g, this, std::placeholders::_1));
}
int g(int) { return a; }
void print() { b.print(a); }
private:
B b;
int a;
};
Note you need to change the type of functor from function pointer to std::function, which is applicable with std::bind.
LIVE
I have a struct type A, which I could instantiate with A(p_a1, p_a2) and a struct type B, which inherits from A, that I can instantiate with B(p_b1, p_b2, p_b3).
Is there a standard way to create a B instance while also setting p_a1 and p_a2? Would I just set them after creating the B instance with b.p_a1 = v_a1; b.p_a2 = v_a2?
(Sorry for any incorrect use of terminology. I don't have much experience with C++.)
Thanks
You are looking for a C++ feature named constructor initialization lists. This example should clarify it to you:
#include <iostream>
class Foo
{
public:
Foo( int x )
{
std::cout << "Foo's constructor "
<< "called with "
<< x
<< std::endl;
}
};
class Bar : public Foo
{
public:
Bar(int x, int y, int z) : Foo( x ) // construct the Foo part of Bar
{
std::cout << "Bar's constructor" << std::endl;
}
};
int main()
{
Bar stool(10,11,12);
}
What you intend to do is achievable using initialization lists and can be done as follows:
struct A
{
A(int _a):a(_a){}
};
struct B : public A
{
B(int _a, int _b):A(a), b(_b){}
} ;
In the sample code which you have provided, what you are doing in these lines b.p_a1 = v_a1; b.p_a2 = v_a2 is called assignment. While the former will invoke just the right constructor, latter would invoke the default constructor.
Try the following
struct A {
int _a1;
int _a2;
A(int a1, int a2); //assume it is defined
};
struct B : public A {
int _b1;
B(int p1, int p2, int p3);
};
B(int p1, int p2, int p3)
: A(p1,p1), // <<== This is where parent member variables will be initialized.
_b1(p3) //or some other way to populate _b1
{ }
google "C++ constructor initialization list"