473,407 Members | 2,320 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,407 software developers and data experts.

Class as data member of another class

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?
Oct 19 '05 #1
5 3772
* 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)
{}
--
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?
Oct 19 '05 #2
"Alf P. Steinbach" <al***@start.no> wrote in message
news:43****************@news.individual.net...
* 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)
{}
--
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.
Oct 19 '05 #3
* 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.

--
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?
Oct 19 '05 #4
"meyousikmann" <me**********@nospamyahoo.com> writes:

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
Oct 19 '05 #5
On Tue, 18 Oct 2005 22:55:37 -0500, meyousikmann wrote:
<snip>

Class2.cpp

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


This assumes Class1 has a well-defined assignment operation.

- Jay
Oct 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: diadia | last post by:
when i write two classes as follow . compiler tell me that i can't access private number from other class but if types of two classes are the same it don't have error message ? why? class...
3
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming...
12
by: Manolis | last post by:
Hi, I was wondering if there is any way to make two objects of the same class to be able to access each other's private data, like this: class A { public: void access( const A& a )...
5
by: Chris | last post by:
Hi, I don't get the difference between a struct and a class ! ok, I know that a struct is a value type, the other a reference type, I understand the technical differences between both, but...
18
by: sd2004 | last post by:
could someone please show/help me to copy all element from "class dog" to "class new_dog" ? Note: "class new_dog" has new element "age" which should have value "my_age"...
7
by: WXS | last post by:
Vote for this idea if you like it here: http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=5fee280d-085e-4fe2-af35-254fbbe96ee9...
4
by: Steve Goldman | last post by:
Even asking this question probably demonstrates that I have a fundamental misunderstanding of how values and references work in C#, but here goes: I'd like to assign a reference to an arbitrary...
2
by: heng | last post by:
If the data member of a class is an array, how to initialize? I tried the following, but it is wrong. class A { public: int a; A():a({0,0,0}){} };
3
by: aine_canby | last post by:
I have a class which has a member which takes a long time to populate. Some of this member is serialised with an xml file, and some is established through a set of commands issued on the system....
20
by: tshad | last post by:
Using VS 2003, I am trying to take a class that I created to create new variable types to handle nulls and track changes to standard variable types. This is for use with database variables. This...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.