identical initialization for distinct classes[was:Iterator of a child class]
I have a 3 classes with realtionship of Base->CORD2C->CORD2R.
class TypeBase
{
private:
bool shortFormat;
string entireLine;
long fieldWidth;
public:
void setFieldWidth(string &);
void toString();
string TypeBase::mergeLines(list<string> &);
string name;
static const long MAXLINELENGTH = 80;
static const long BLANKSPACE = -99999;
friend class TypeCORD2C;
friend class TypeCORD2R;
};
Cord2c constructor takes a string and puts the numbers in its proper variable.
class TypeCORD2C: public TypeBase
{
public:
long CID;
long RID;
float A[3];
float B[3];
float C[3];
TypeCORD2C(string &);
friend class TypeCORD2R;
};
class TypeCORD2R: public TypeCORD2C
{
public:
TypeCORD2R(string &);
};
CORD2R is exactly the same as CORD2C so all initilzation are done by the parent. Is this the right way to write this child constructor?
TypeCORD2R::TypeCORD2R(string & data):TypeCORD2C(data)
{
//no code needed, all work done by parent
}
the problem occurs when I get to this line in my code:
vector<TypeCORD2R>::iterator itrCORD2R = unique(masterCORD2R.begin(),masterCORD2R.end());
compiler complains and gives me:
error C2440: 'initializing' : cannot convert from 'std::vector<_Ty>::iterator' to 'std::vector<_Ty>::iterator'
with
[
_Ty=TypeCORD2C
]
and
[
_Ty=TypeCORD2R
]
No constructor could take the source type, or constructor overload resolution was ambiguous
am I missing some constructor?
[1807 byte] By [
rssmps] at [2007-11-11 7:33:30]

# 1 Re: identical initialization for distinct classes[was:Iterator of a child class]
You can't do that. The fact the two classes happen to have the same members doesn't mean that they are the same thing. Does every class that has an int member is considered the same type? Since TypeCORD2R isn't derived from TypeCORD2C, you can't initialize the latter from the former's constructor -- you can only do that if TypeCORD2R is derived from TypeCORD2C.
The real question is of course, why use two different classes if they are the same? If you want to abstract the initialization operation, I suggest that you add an intermediary base class that has all the data members of TypeCORD2C, and -R, initializes it in its constructor, and then inherit TypeCORD2C and -R from that base class.
Danny at 2007-11-11 21:02:10 >

# 2 Re: identical initialization for distinct classes[was:Iterator of a child class]
Since TypeCORD2R isn't derived from TypeCORD2C, you can't initialize the latter from the former's constructor -- you can only do that if TypeCORD2R is derived from TypeCORD2C.
Did I inherit incorrectly? TypeCORD2R should be inheriting from TypeCORD2C
isn't that what this code is doing?
class TypeCORD2R: public TypeCORD2C
{
public:
TypeCORD2R(string &);
};
rssmps at 2007-11-11 21:03:07 >

# 4 Re: identical initialization for distinct classes[was:Iterator of a child class]
BTW, I don't want to sund nit-picky but your naming convention has more than once failed me, so you really want to use more self-describing, unmabiguous names for classes. It was a serious challenge for me to decipher your vector quey on a previous thread, 90% of the effort was dedicated to understanding what those peculiar names meant. Same here. If the two classes are different, give them meaningful and distinct names. It's simple, and it's a key for writing less bugs...
Danny at 2007-11-11 21:05:12 >

# 5 Re: identical initialization for distinct classes[was:Iterator of a child class]
Yeah, I can see that. This application is for Nastran/Patran so the class names will only make sense if you know about these application. I don't have that many classes, just a ModelFile and some card classes (RBE2, PELAS, CORD2C, CORD2R...etc.)
What's really confusing is all the different vectors that I create to manipulate these objects. I agree that could all be made more clear.
Thanks for the tip!
rssmps at 2007-11-11 21:06:16 >
