By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,197 Members | 1,173 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,197 IT Pros & Developers. It's quick & easy.

Object member with constructor parameter?

P: n/a
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

Apr 25 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
kk****@yahoo.com wrote:
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
--
Please remove capital As from my address when replying by mail
Apr 25 '06 #2

P: n/a
kk****@yahoo.com wrote:
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
Apr 25 '06 #3

P: n/a

Mark P wrote:
kk****@yahoo.com wrote:
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


Thanks!

Apr 25 '06 #4

P: n/a
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!.

Apr 25 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.