I'm trying to learn to c++ after programming in other OO languages for many years.
I'm trying to create a wrapper class for another class, but having a hard time figuring out how to set this up properly.
For instance, with the following...
main.cpp
#include "foo.cpp"
#include <iostream>
int main() {
Foo foo(42);
std::cout << foo.get_barx() << std::endl;
return 0;
}
foo.cpp
#include "bar.cpp"
class Foo {
public:
// I'm trying to declare the member variable `m_bar` here. I
// don't want to be instantiating an instance of Bar yet,
// but I think that might be exactly what's happening.
Bar m_bar;
Foo(int y) {
// Here's where I really want to instantiate an instance of Bar
// and assign it to m_bar.
Bar m_bar(y*2);
}
int get_barx() {
return m_bar.getx();
}
};
bar.cpp
class Bar {
public:
int m_x;
// I seem to need this default constructor for the declaration
// of `m_bar` above, but I don't think that line should be
// calling any constructors.
Bar() { m_x = 21; };
Bar(int x) {
m_x = x;
}
int getx() {
return m_x;
}
};
When I compile and run this, I get back 21, but I expect 84. I'm pretty sure I'm doing something fundamentally wrong, and I'm pretty sure it's got something to do with how I'm declaring the m_bar member variable in Foo, but I can't figure out what the right way is to accomplish this.
main.cpp
#include "foo.cpp"
#include <iostream>
int main()
{
Foo foo(42);
std::cout << foo.get_barx() << std::endl;
return 0;
}
Here you should be including a header (e.g., rename "foo.cpp" to "foo.h"). In general, the header provides the declaration and the source (e.g., .cpp file) provides the definition/implementation.
Bar.cpp (again this should be a header)
class Bar
{
public:
int m_x;
// A default constructor is not required, however defining any constructor
// prevents auto generation of the default constructor
Bar(int x) : // This starts the initializer list section
m_x(x)
{
// This is assignment not initialization
// m_x = x;
}
// See the trailing 'const', research const correctness
int getx() const
{
return m_x;
}
};
foo.cpp (again this should be a header)
#include "bar.cpp"
class Foo
{
public:
// Just declaring a `Bar` data member
Bar m_bar;
Foo(int y) :
m_bar(y) // Initialize `Bar` data member using the available constructor
{
// First, this declares a new `Bar` instance which is different than
// the class member, regardless of the fact they are named the same
// Bar m_bar(y*2);
// Furthermore, even if you did the following it is assignment not initialization
// m_bar = Bar(y*2);
// Since initialization already occurred before this point an error
// will result if the `Bar` data member isn't instantiated via the
// only available constructor, since there isn't a default constructor as
// explained above
}
// Same comment about const correctness
int get_barx() const
{
return m_bar.getx();
}
};
You need to use initialization lists to construct class members at construction time. The body of the constructor gets called after all members have been constructed. If you don't explicitly call a non-default constructor then the compiler will insert a call to the default constructor for you. For example, your Foo class could look like:
class Foo {
public:
Foo(int y) : m_bar(y*2) {}
...
private:
Bar m_bar;
}
Related
Lippman 5th
ISBN-13: 978-0321714114
Page 280-281, it says:
Making A Member Function a Friend
Rather than making the entire Window_mgr class a friend, Screen can
instead specify that only the clear member is allowed access. When we
declare a member function to be a friend, we must specify the class of
which that function is a member:
class Screen {
// Window_mgr::clear must have been declared before class Screen
friend void Window_mgr::clear(ScreenIndex);
// ... rest of the Screen class
};
Making a member function a friend requires careful structuring of our
programs to accommodate interdependencies among the declarations and
definitions. In this example, we must order our program as follows:
First, define the Window_mgr class, which declares, but cannot define, clear. Screen must be declared before clear can use the
members of Screen.
Next, define class Screen, including a friend declaration for clear.
Finally, define clear, which can now refer to the members in Screen.
The problem is: class Window_mgr has a data member that depends of class
Screen definition. See:
class Window_mgr {
public:
// location ID for each screen on the window
using ScreenIndex = std::vector<Screen>::size_type;
// reset the Screen at the given position to all blanks
void clear(ScreenIndex);
private:
std::vector<Screen> screens{Screen(24, 80, ' ')};
};
So it is impossible firstly define Window_mgr without defining Screen
previously!
And at the same time, it is impossible define Screen without we have
defined Window_mgr!!!
How can this problem be solved???
Is the book wrong?
I will paste here a code so that you can repeat the problem using a
minimal code:
#include <iostream>
#include <string>
#include <vector>
class A
{
friend void B::hello();
public:
A(int i) : number{i} {}
private:
void f() {
std::cout << "hello" << std::endl;
}
int number;
};
class B {
private:
std::vector<A> x{A(10)};
public:
void hello()
{
for(A &elem : x)
{
elem.f();
}
}
};
int main()
{
A x;
return 0;
}
If I compile this code, the result is:
error: use of undeclared identifier 'B'
friend void B::hello();
And if I invert the position (A <--> B), I have:
error: use of undeclared identifier 'A'
std::vector x{A(10)};
Is there a correct way to do that??
Thank you!
EDIT:
Thank you, Craig Young
Solution:
#include <iostream>
#include <string>
#include <vector>
class A;
class B {
private:
std::vector<A> x;
public:
B();
void hello();
};
class A
{
friend void B::hello();
public:
A(int i) : number{i} {}
private:
void f() {
std::cout << "hello" << std::endl;
}
int number;
};
B::B() : x{A(10)}
{
}
void B::hello()
{
for(A &elem : x)
{
elem.f();
}
}
int main()
{
return 0;
}
Conclusion:
the book is incomplete in that it doesn't expose the necessity of doing the forward declaration of class A firstly and the impossibility to do in-class initialization in this case.
I didn't notice that the problem was the A(10), not the vector! That is, we can use incomplete type A (only declaration, without definition) when we are using it as Template argument to vector (because it doesn't create A object itself) but we can not use incomplete type A when defining a object, for example: A(10);
For a start
Well, you're not following the guidance correctly.
First, define the Window_mgr class, which declares, but cannot define, clear. Screen must be declared before clear can use the members of Screen.
You must declare B before A.
Next, define class Screen, including a friend declaration for clear.
Now declare A with B::hello() as a friend.
Finally, define clear, which can now refer to the members in Screen.
B:hello() can use the private members of A.
This has been covered before here: C++ Forward declaration , friend function problem
You've added complications
Furthermore you want declarations of B to reference A. To achieve this you need to forward declare A so that B knows of its existence.
And it's important to be aware that you have only "partial" access to A. You cannot 'fully use' A in the declaration of B. So the following line in B is wrong.
//You're trying to create A when you only know it exists.
//You don't have a full definition of A yet.
std::vector<A> x{A(10)};
//Replace the above with...
std::vector<A> x;
Of course you'll have to find another way to initialise x.
Sample code
#include <iostream>
#include <vector>
class A;
class B
{
private:
std::vector<A> x;
public:
void hello();
};
class A
{
friend void B::hello();
public:
A(int i): number(i) {}
private:
void f() { std::cout << "hello" << std::endl; }
int number;
};
void B::hello()
{
for(A &elem : x)
{
elem.f();
}
}
int main()
{
A a{5};
return 0;
}
You have to have an earlier declaration, but not an earlier definition.
Adding
class A;
class B;
at the front tells the compiler that “A” and “B” refer to classes. That should be enough for it to reason out the rest.
I've these pointer declarations objects within the .h:
ILFO *pLFOPianoRoll1, *pLFOPianoRoll2, *pLFOPianoRoll3;
which I init in the .cpp with:
pLFOPianoRoll1 = new ILFO(this, 8, 423, kParamIDPianoRollLFO1, 0);
pLFOPianoRoll2 = new ILFO(this, 8, 542, kParamIDPianoRollLFO1, 1);
pLFOPianoRoll3 = new ILFO(this, 8, 661, kParamIDPianoRollLFO1, 2);
but I'd like to avoid pointers here (I learnt that "if you don't need them, don't use them"), and just use variable/class (due to manual management of the memory later).
But how can I decleare the variable of the object in the .h (such as ILFO mLFOPianoRoll1) and than call the CTOR on the .cpp?
You can use initialization list for this purpose.
#include <iostream>
#include <string>
using namespace std;
class A
{
public:
A(int a_) : a(a_) { }
void print()
{
std::cout << "A: " << a << std::endl;
}
int a;
};
class B
{
public:
B() : a(1), a2(3) {}
A a;
A a2;
};
int main() {
B bObj;
bObj.a.print();
bObj.a2.print();
return 0;
}
https://ideone.com/C7Vx1X
In order to simply declare the variable, use the extern keyword:
extern ILFO obj; //just declaration, no constructor is called.
in the .cpp file
ILFO obj(blah, blah, blah); //definition
This, of course, if you're talking about namespace-scope (including global) variables. If you're talking about class members, then you must know that the constructors of the members are not invoked until the constructor of the class. You can pass the parameters to the constructors in the constructor initialization list.
I have a forward declaration but want to have a instatance of the forward declarated class as default value.
So my code looks more or less like this:
bar.h
class Foo;
class Bar
{
public:
Bar();
void test(Foo foo = Foo(7));
}
and foo.h
class Foo
{
public:
Foo(int val);
}
Is this even possible?
No. When you declare a method/function which argument is Foo the compiler should already know the size of Foo, so it should be already defined at that moment. If you declared but not defined Foo you may use only pointers or references to it.
It's impossible as described in another answer.
Apart of that, default values are the code smell. They make your interfaces fragile
Use method overloading instead:
class Foo;
class Bar
{
public:
Bar();
void test(); // implement with default value, and document it
void test(Foo foo); // implement with user-supplied value
}
In bar.cpp:
#include "foo.h" // Complete Foo is available here
void Bar::test()
{
test(Foo(42));
}
void Bar::test(Foo foo)
{
...
}
How could I make a function only seen by the function that calls it?
define the function I want to hide as private function is not enough, as it could still be seen by other public functions in the class.
Now I use lambda expression to define anonymous function inside function. Is there any better solution?
Aside from using a lambda (which you've rejected), you could implement your function in its own compilation unit, and code the supporting function in an anonymous namespace within that compilation unit.
But that supporting function would be outside the class, so you'd have to pass it all the parameters it needed. That could become unwieldly though no worse than a long lambda capture list.
You can use a function object. For example(you can compile this, even in C++03):
#include <iostream> // only for output
class foo{
int bar(){return 0;} // Only foo can see this
public:
int operator()(){
return bar();
}
};
class baz{
public:
foo do_foo;
};
int main(){
baz a;
std::cout << a.do_foo() << std::endl;
}
the method bar is only visible by a foo.
P.S.: If you need foo to access members of baz, make it a friend.
A simmilar approach to cassiorenan would be to use static class functions and friends.
Something like this:
void Boss();
class Worker {
static void Test(){ return;}
friend void Boss();
};
void Boss(){
Worker::Test();
}
Though why would you want to do this, I don't know.
It is possible to define function inside a function without lambdas. Just define a struct that contains required function. This approach is not much better than using lambda, but at least this is straightforward and works with older compilers too.
int func() {
struct {
int hiddenFunc() {
return 1;
}
} h;
int a = h.hiddenFunc() + h.hiddenFunc();
return a;
}
As a slight variation from cassiorenan's solution, you could use a class containing one public static function (the visible function) and one static private function that could only be called from there. To avoid creation of objects of that class, it is enough to put a private constructor.
EDIT:
Per cassiorenan's comment, I can see that OP really needs methods and not functions. In that case, I would still use a dedicated class in a anonymous namespace to ensure it is not visible from elsewhere (even if my example is single file ...) friend to the class really used. So in below example, bar is the business class that would have a method with an externally hidden implementation (here relay_method), and foo is dedicated to the hidden method called with a pointer to the real object. In real world, the whole anonymous namespace and the implementation of the hidden method should be in the implementation file bar.cpp.
That way, the real implementation function priv_func can only be called from a bar object through bar::relay_method() and foo::bar_func(bar &).
#include <iostream>
class bar;
namespace {
class foo {
private:
static int priv_func(int i) {
return i * i;
}
foo() {}
public:
// only useful if true functions were needed
/* static int pub_func(int i, int j) {
return priv_func(i) + priv_func(j);
}*/
static void bar_func(bar& b);
};
}
class bar {
int x;
int x2;
public:
bar(int i): x(i) {}
void relay_method() {
foo::bar_func(*this);
}
friend class foo;
int getX2() const {
return x2;
}
};
void foo::bar_func(bar& b) {
b.x2 = foo::priv_func(b.x);
}
using namespace std;
int main() {
/* int i = foo::pub_func(3,4);
cout << i << endl;
// foo::priv_func(2); error access to private member of class foo
// foo f; */
bar b(2);
b.relay_method();
cout << b.getX2() << endl;
return 0;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
What is the syntax for dynamically allocating an object with an overloaded constructor in C++?
If I have a class Foo:
class Foo{
public:
Foo(string str, int nbr); // Overloaded constructor
};
And a second class Foo2 (using Foo):
#include "Foo"
class Foo2{
public:
Foo* myFoo; // Wrong!
Foo2(){
myFoo = new Foo(myStr, myNbr);
}
};
The error displayed is the following :
no matching function for call to "Foo::Foo()"
How can I, when creating myFoo object pointer, specify that it will use the Foo(string str, int nbr) constructor, not Foo() constructor.
Is it possible without using constructor delegation?
Your syntax to construct the object is correct.
It's hard to say for sure since you haven't told the error, but my guess is that your problem is that the constructor is private. That means you cannot use the constructor outside the class.
Edit concerning the error message:
Here's a complete example that compiles. I've added some example lines that would produce the error: no matching function for call to 'Foo::Foo()'.
#include <string>
class Foo{
public:
Foo(std::string str, int nbr);
};
// empty definition
Foo::Foo(std::string str, int nbr) {}
// delegating constructor (c++11 feature)
// this would produce error: no matching function for call to 'Foo::Foo()'
//Foo::Foo(std::string str, int nbr) : Foo::Foo() {}
int main() {
Foo* myFoo;
myFoo = new Foo("testString", -1); // this is correct
// trying to construct with default constructor
//Foo myFoo2; // this would produce error: no matching function for call to 'Foo::Foo()'
//Foo* myFoo3 = new Foo(); // so would this
}
Given the error, your code is trying to use the default constructor somewhere.
Edit2 concerning your new Foo2 example. Your declaration of Foo* and the call to the constructor are still correct and the code should compile if you fix the method visibility and missing semicolons. Following example compiles:
#include <string>
class Foo{
public:
Foo(std::string str, int nbr); // Overloaded constructor
};
Foo::Foo(std::string str, int nbr){}
class Foo2{
Foo* myFoo; // This is still correct
public:
Foo2() {
myFoo = new Foo("", 1);
}
};
int main() {
Foo2 foo2;
}
Syntax is correct.
There are many possibilities as you have not written full class definition.
1. check whether default cnstsructor is written.
2. Check whether both constructors are inside public section.
3. Alternatively you can change call to constructor as below,
Foo* myFoo = new Foo("testString", -1);
The following code should work.
class Foo
{
string str;
int num;
public:
Foo(string p_str, int nbr):str(p_str),num(nbr){}; // Overloaded constructor
Foo() {}; // Default constructor
};
The correct way is initializing the members in each constructor. You could extract their common code in a private init() member function and call it in each constructor like the following:
class Foo {
public:
Foo(string x);
Foo(string x, int y);
...
private:
void init(string x, int y);
};
Foo::Foo(string x)
{
init(x, int(x) + 3);
...
}
Foo::Foo(string x, int y)
{
init(x, y);
...
}
void Foo::init(string x, int y)
{
...
}