0

I have some understanding problems in C++ with parameterised constructors. If I have a class with one constructor which have two function parameters, how can i instantiate it in an other class header file?

For example:

public:
      MyFunction myfunction(param1, param2); //makes an error

How can i declare an object like this?

2
  • 1
    "//makes an error" ... what error? That doesnt look like a proper declaration, or are param1 and param2 types? Commented Mar 31, 2016 at 9:57
  • "Expected a type specifier" Commented Mar 31, 2016 at 10:12

4 Answers 4

3

You need to write MyFunction myfunction; in the class declaration.

Then in the member initialiser list of the constructor to the class of which myfunction is a member, write

/*Your constructor here*/ : myfunction(param1, param2)
{
    /*the constructor body*/
}

The bit after the colon is the member initialiser list. param1 and param2 are obviously arguments to that constructor.

Sign up to request clarification or add additional context in comments.

1 Comment

Better answer than mine, and much more optimized
1

You have several ways:

struct P
{
    P(int a, int b) :a(a), b(b){}

    int a;
    int b;
};

struct S
{
    S() : p1(4, 2) {} // initializer list

    P p1;
    P p2{4, 2}; // since c++11
    P p3 = P(4, 2); // since c++11
};

2 Comments

I like this a lot; but is it worth pointing out that P p2{4, 2} actually calls the constructor, unlike C?
Hmmm, nice, i will try this. What happens (in the background) if i use this syntax?
0

An option is having a predeclaration and a pointer to the class.

classA.h
class A
{
    public:
    A(a,b);
};

classB.h
class A;
class B{
    public:
    A* foo;
    B();
    ~B();
}

classB.cpp
#include classA.h
#include classB.h
B()
{
    foo=new A(a,b);
}
~B()
{
    delete(foo);
}

Comments

0

As far as I understand, the problem is that MyFunction has only one constructor that only accepts two arguments.

You can use a pointer to that object and then initialize it in the constructor:

#include <memory>

class Something {
    public:
        std::shared_ptr<MyFunction> data;
        Something(int par1, int par2) {
            data = std::shared_ptr<MyFunction>(new MyFunction(par1, par2)); // here you go!
        }

        ~Something() {
            //delete data; no need to do this now
        }
};

Edit: added a smart pointer to follow rule of three.

3 Comments

@Jarod42, I'm new to C++, so my question might sound silly: what is 'rule of 3/5/0'?
Yeah it's a nice way :) But this is the only solution? Use pointers / smart poinsters for this?
no, it's not the only one. See the answer of @Bathsheba for another way to do this. The way shown in my answer is the simplest one, to my mind.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.