implementing composition

C

ccs

1. How to make the code work?

class CTest
{
public:
int n;
CTest(int v) : n(v) { }
};

class CContainer
{
int nn;
private:
vector<CTest> Array( nn, CTest(8) );
public:
CContainer(int v) : nn(v) {}
};

int main(void)
{
CContainer cc(50);
...
}

2. Compared to the above design of CContainer class, what benefits does the
following one have?

class CContainer
{
int nn;
private:
vector<CTest*> ptrToArray;
public:
CContainer(int v) : nn(v) {}

};

Thanks in advance!
 
M

Mike Wahler

ccs said:
1. How to make the code work?

class CTest
{
public:
int n;
CTest(int v) : n(v) { }
};

class CContainer
{
int nn;
private:
vector<CTest> Array( nn, CTest(8) );
public:
CContainer(int v) : nn(v) {}
};

int main(void)
{
CContainer cc(50);
...
}

Before you can get it to 'work', first you must get it to compile.
The above does not.

#include <vector>

class CTest
{
public:
int n;
CTest(int v) : n(v) { }
};

class CContainer
{
int nn;
private:
std::vector<CTest> Array;
public:
CContainer(int v) : nn(v), Array( v, CTest(8) ) {}
};

int main(void)
{
CContainer cc(50);
return 0;
}

Now decide what it means to 'work', and make it do that.
2. Compared to the above design of CContainer class, what benefits does the
following one have?

class CContainer
{
int nn;
private:
vector<CTest*> ptrToArray;

Poor identifier name. This is not a pointer to an 'Array',
but an 'Array' (actually a vector) of pointers (to type 'CTest').
public:
CContainer(int v) : nn(v) {}

};

You can't determine if it has any 'benefits' at all until
you define your requirements. The first program implements
a container of 'CTest' objects. The second one implements
a container of pointers to 'CTest' objects. Which do you need?
Why?

Does not using a std::vector directly serve your needs rather
than wrapping one in a custom type? Why?

-Mike
 
D

David Rubin

ccs said:
1. How to make the code work?
class CTest
{
public:
int n;
CTest(int v) : n(v) { }
};
class CContainer
{
int nn;
private:
vector<CTest> Array( nn, CTest(8) );

vector said:
public:
CContainer(int v) : nn(v) {}

CContainer(int v);

inline
CContainer::CContainer(int v)
: nn(v), Array(nn, CTest(8))
{
}
int main(void)
{
CContainer cc(50);
...
}
2. Compared to the above design of CContainer class, what benefits does the
following one have?
class CContainer
{
int nn;
private:
vector<CTest*> ptrToArray;
public:
CContainer(int v) : nn(v) {}

};

A vector of pointer-to-CTest allows you to call polymorphic functions
of CTest in CContainer (or elsewhere, given adequate exposure of
Array). However, CContainer most likely will require clients to
guarantee the lifetime of Array elements, and clients are responsible
for destroying Array elements. The first design stores copies of CTest
instances (requiring a valid CTest copy constructor). /david
 

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
474,170
Messages
2,570,925
Members
47,464
Latest member
Bobbylenly

Latest Threads

Top