Class as data member of another class

M

meyousikmann

Given these two (incomplete but representative) classes in two seperate
header files:

Class1.h

class Class1
{
public:
Class(const char CharValue, const int IntValue1, const int IntValue2);
~City();
private:
};


Class2.h

#include "Class1.h"

class Class2
{
public:
Class2(const int IntValue1, string StringValue);
~Class2();
private:
Class1 Class;
};


and an implementation file for Class2:

Class2.cpp

Class2::Class2(const int IntValue1, string StringValue)
{
Class('A', 30, 30); // trying to create an object of type Class1 within
the constructor of Class2
}


notice the data member 'Class1 Class' in Class2. What I am trying to
accomplish is to have a data member in Class2 that is of type Class1 and
create that object of type Class1 within the constructor of Class2. The
code in this form won't compile and I am not sure what to do now. I am new
to C++ and I can't seem to get this to work. Anyone help me?
 
A

Alf P. Steinbach

* meyousikmann:
Given these two (incomplete but representative) classes in two seperate
header files:

Class1.h

class Class1
{
public:
Class(const char CharValue, const int IntValue1, const int IntValue2);
~City();
private:
};

The ~City should not compile.


Class2.h

#include "Class1.h"

class Class2
{
public:
Class2(const int IntValue1, string StringValue);
~Class2();
private:
Class1 Class;
};


and an implementation file for Class2:

Class2.cpp

Class2::Class2(const int IntValue1, string StringValue)
{
Class('A', 30, 30); // trying to create an object of type Class1 within
the constructor of Class2
}


notice the data member 'Class1 Class' in Class2. What I am trying to
accomplish is to have a data member in Class2 that is of type Class1 and
create that object of type Class1 within the constructor of Class2. The
code in this form won't compile and I am not sure what to do now. I am new
to C++ and I can't seem to get this to work. Anyone help me?

To initialize the member Class (please do something about your currently
confusing naming convention!) use a constructor initializer list, like so:

Class2::Class2(const int IntValue1, string StringValue)
: Class('A', 30, 30)
{}
 
M

meyousikmann

Alf P. Steinbach said:
* meyousikmann:

The ~City should not compile.




To initialize the member Class (please do something about your currently
confusing naming convention!) use a constructor initializer list, like so:

Class2::Class2(const int IntValue1, string StringValue)
: Class('A', 30, 30)
{}


--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Thanks so much for your reply. Appreciated.

With regard to my confusing naming convention, none of the variable names,
class names, etc... are actually the real names that I am using I was only
trying to simplify the problem by showing an incomplete "generic" example of
what I am trying to accomplish rather than get bogged down in variable names
and class names.

With regard to the constructor initializer, I failed to make my example
clear.....apologies. I can't use an initializer because I need to create
the Class object within the Class2 constructor using initializing parameters
that are read from a text file. Here is how it really should look (using
psuedocode in places to convey functionality):

Class2::Class2(const int IntValue1, string StringValue)
{
<psuedocode> read in a data file to get three parameters needed (const
char CharValue, const int IntValue1, const int IntValue2) to create and
initialize the Class object </psuedocode>

Class(CharValue, IntValue1, IntValue2);
}

Clear as mud? I hope I haven't totally confused the situation. Again,
thanks for replies.
 
A

Alf P. Steinbach

* meyousikmann:
[excessive quoting, including signature]

Please don't.

With regard to my confusing naming convention, none of the variable names,
class names, etc... are actually the real names that I am using I was only
trying to simplify the problem by showing an incomplete "generic" example of
what I am trying to accomplish rather than get bogged down in variable names
and class names.

Do post real code that compiles.

Or, if a compilation error is the problem, real code that shows the problem
exactly.

The smalles such example you can make -- but it's got to be real, because
none of us are telepaths.

Class2::Class2(const int IntValue1, string StringValue)
{
<psuedocode> read in a data file to get three parameters needed (const
char CharValue, const int IntValue1, const int IntValue2) to create and
initialize the Class object </psuedocode>

Class(CharValue, IntValue1, IntValue2);
}

Clear as mud?

Yes, it's a design problem, but you insist on a C++ technical solution.

There are a number of such solutions, and the easiest in your case would
probably to call a member function that produces the argument set.

static Class2InitArgs argumentsFromFile() { ... }

Class2::Class2(const int IntValue1, string StringValue)
: Class( argumentsFromFile() )
{}

Alternatively you can use a std::auto_ptr<Class2> as member and dynamically
the object in your constructor body. Then you'll have to define assignment
and copy construction. Or remove these operations.
 
N

Niklas Norrthon

With regard to the constructor initializer, I failed to make my example
clear.....apologies. I can't use an initializer because I need to create
the Class object within the Class2 constructor using initializing parameters
that are read from a text file. Here is how it really should look (using
psuedocode in places to convey functionality):

Class2::Class2(const int IntValue1, string StringValue)
{
<psuedocode> read in a data file to get three parameters needed (const
char CharValue, const int IntValue1, const int IntValue2) to create and
initialize the Class object </psuedocode>

Class(CharValue, IntValue1, IntValue2);
}

Perhaps using a pointer, and delaying the creation is a possibility:

#include <memory>

class A {
public:
A(char c, int i1, int i2) : m_c(c), m_i1(i1), m_i2(i2) { }
~A() { }
private:
char m_c;
int m_i1;
int m_i2;
};

class B {
public:
B(int i, std::string s);
~B();

private:
std::auto_ptr<A> m_a;
};

B::B(int i, std::string s) : m_a(0)
{
char c;
int i1, i2;

// Do whatever to calculate c, i1, and i2
m_a = new A(c, i1, i2);
}

/Niklas Norrthon
 
J

Jay Nabonne

On Tue, 18 Oct 2005 22:55:37 -0500, meyousikmann wrote:
Class2.cpp

Class2::Class2(const int IntValue1, string StringValue)
{

Class = Class1('A', 30, 30);

This assumes Class1 has a well-defined assignment operation.

- Jay
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top