Object member with constructor parameter?

K

kk_oop

Hi. I want class B to have a member of class A. Class A's constructor
takes an int parameter. Here are the classes:

Here's A.h*****************
#ifndef A_H_
#define A_H_

class A
{
public:
A(int theX);
virtual ~A();
private:
int x;
};

#endif /*A_H_*/

Here's B.h********************

#include "A.h"

class B
{
public:
B();
virtual ~B();

A a(10); //this causes an error
};

#endif /*B_H_*/

*********************

The declaration of A gives me an error. What is the correct way to do
this?

Thanks!

Ken
 
V

Victor Bazarov

Hi. I want class B to have a member of class A. Class A's
constructor takes an int parameter. Here are the classes:

Here's A.h*****************
#ifndef A_H_
#define A_H_

class A
{
public:
A(int theX);
virtual ~A();
private:
int x;
};

#endif /*A_H_*/

Here's B.h********************

#include "A.h"

class B
{
public:
B();
virtual ~B();

A a(10); //this causes an error
};

#endif /*B_H_*/

*********************

The declaration of A gives me an error. What is the correct way to do
this?


Initialisation can only be done in the constructor initialiser list.
Google for "constructor initializer list".

V
 
M

Mark P

Hi. I want class B to have a member of class A. Class A's constructor
takes an int parameter. Here are the classes:

Here's A.h*****************
#ifndef A_H_
#define A_H_

class A
{
public:
A(int theX);
virtual ~A();
private:
int x;
};

#endif /*A_H_*/

Here's B.h********************

#include "A.h"

class B
{
public:
B();
virtual ~B();

A a(10); //this causes an error
};

#endif /*B_H_*/

*********************

The declaration of A gives me an error. What is the correct way to do
this?

Non-static member initialization is always done within the constructor.
For a member which does not have a default constructor (i.e., cannot
be constructed without supplying one or more parameters), this must be
done in the initializer list.

You can define B::B() as,
B::B() : a(10) {/* ctor body */}

You also must remove the argument from the declaration of 'a' within the
definition of B.

-Mark
 
K

kk_oop

Mark said:
Non-static member initialization is always done within the constructor.
For a member which does not have a default constructor (i.e., cannot
be constructed without supplying one or more parameters), this must be
done in the initializer list.

You can define B::B() as,
B::B() : a(10) {/* ctor body */}

You also must remove the argument from the declaration of 'a' within the
definition of B.

-Mark

Thanks!
 
L

LordFord

Well, if you want to construct an A object and returned it in a(...)
function then you may change your declaration:

Here's B.h********************


#include "A.h"


class B
{
public:
B();
virtual ~B();


A a(int param){ A obj(param); return obj;}; // this is correct


};


#endif /*B_H_*/


*********************


Get it ?

See you!.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top