473,378 Members | 1,375 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Object member with constructor parameter?

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
4 1743
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
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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Glenn Serpas | last post by:
I have Class A and Class B .. Class B has a private member that is a pointer to a Class A object. private: B *mypointer ; I instantiate the A object A* myobject new = A();
12
by: Olumide | last post by:
I'm studying Nigel Chapman's Late Night Guide to C++ which I think is an absolutely fantastic book; however on page 175 (topic: operator overlaoding), there the following code snippet: inline...
1
by: Bo Xu | last post by:
Object of Combination By Bo Xu Introduction A combination of n things, taken s at a time, often referred as an s-combination out of n, is a way to select a subset of size s from a given set of...
5
by: surrealtrauma | last post by:
the requirement is : Create a class called Rational (rational.h) for performing arithmetic with fractions. Write a program to test your class. Use Integer variables to represent the private data...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
12
by: Belebele | last post by:
Suppose that a class A depends on class B because an object of class B is passed into A's constructor. See below: class B { ... }; class A { public: A(B const& b); ... };
8
by: v4vijayakumar | last post by:
Is there any way to defer instantiation of " t1" to the constructor of "test1"? because, "t1" can only meaningfully initialized in the "test1" constructor. #cat test.cpp #include <iostream>...
14
by: Jeroen | last post by:
Hi all, I've got a question about writing a library. Let me characterize that library by the following: * there is a class A which is available to the user * there is a class B that is used...
4
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.