templates - circular

E

er

Hi all,

1)

I have N=2 distinct pairs of objects {(x0,y0),(x1,y1)} related as

x0 -> y0
y0 -> {x1}
x1 -> y1
y1 -> {x0}

where -> represents, for example, has-a-ptr-to. Classes of objects
labeled x# and y# may have to support some operations (to safisfy
their left or right neighbors) but are of arbitrary type. So a class
for x# has to be like template<class Ty> class M_x{}, but also
template<class Tx> class M_y{},
which runs into a circular problem.

Any suggestion, please?

2) How about N>2? for example N=3,

x0 -> y0
y0 -> {x1,x2}
x1 -> y1
y1 -> {x0,x2}
x2 -> y2
y2 -> {x0,x1}
 
V

Victor Bazarov

er said:
Hi all,

1)

I have N=2 distinct pairs of objects {(x0,y0),(x1,y1)} related as

x0 -> y0
y0 -> {x1}
x1 -> y1
y1 -> {x0}

where -> represents, for example, has-a-ptr-to. Classes of objects
labeled x# and y# may have to support some operations (to safisfy
their left or right neighbors) but are of arbitrary type. So a class
for x# has to be like template<class Ty> class M_x{}, but also
template<class Tx> class M_y{},
which runs into a circular problem.

Any suggestion, please?

2) How about N>2? for example N=3,

x0 -> y0
y0 -> {x1,x2}
x1 -> y1
y1 -> {x0,x2}
x2 -> y2
y2 -> {x0,x1}

Is this homework? Even if it isn't, would you perhaps show us what
you already have?

V
 
E

er

Is this homework? Even if it isn't, would you perhaps show us what
you already have?

V

What I already have is a specific configuration {(M_x_fc,M_y_fc),
(M_x_fc,M_y_fc)} (see below). What i'd like to able to do is have an
arbitrary configuration, so long as the required operations are
supported. For example, M_x_fc requires that r_type support mean(),
and M_y_fc requires that l_type support x(); Please ask if still not
clear.


class M_x_fc{
public:
void update(double);
double x()const;
typedef M_y_fc r_type;
typedef boost::shared_ptr<const r_type> ptr_const_r_type;

private:
ptr_const_r_type ptr_const_r;
double _x;
};
void M_x_fc::update(double x_){
_x = x_;
};
double M_x_fc::mean()const{return ptr_const_r->mean();};


class M_y_fc{
public:
typedef M_x_fc l_type;
typedef boost::shared_ptr<const l_type> ptr_const_l_type;
void set(ptr_const_l_type ptr_l_compl);
double mean()const;
private:

double _rho;
double _mean;
ptr_const_l_type ptr_l_compl;
};
void M_y_fc::update(){
_mean = _rho*(ptr_l_compl->x());
};

double M_y_fc::mean()const{return _mean;};


class Config_fcfc{
public:
typedef M_x_fc l_fc_type;
typedef M_y_fc r_fc_type;
typedef boost::shared_ptr<l_fc_type> ptr_l_fc_type;
typedef boost::shared_ptr<r_fc_type> ptr_r_fc_type;
Config_fcfc(double rho);

ptr_r_fc_type ptr_r_fc_0;
ptr_r_fc_type ptr_r_fc_1;
ptr_l_fc_type ptr_l_fc_0;
ptr_l_fc_type ptr_l_fc_1;
};

Config_fcfc::Config_fcfc(double rho):
ptr_r_fc_0( new r_fc_type(0,rho)),
ptr_r_fc_1( new r_fc_type(1,rho)),
ptr_l_fc_0( new l_fc_type(ptr_r_fc_0)),
ptr_l_fc_1( new l_fc_type(ptr_r_fc_1))
{
ptr_r_fc_0->set(ptr_l_fc_1);
ptr_r_fc_1->set(ptr_l_fc_0);
};
 
E

er

What I already have is a specific configuration {(M_x_fc,M_y_fc),
(M_x_fc,M_y_fc)} (see below). What i'd like to able to do is have an
arbitrary configuration, so long as the required operations are
supported. For example, M_x_fc requires that r_type support mean(),
and M_y_fc requires that l_type support x(); Please ask if still not
clear.

                class M_x_fc{
                        public:
                                void update(double);
                                double x()const;
                                typedef M_y_fc                                          r_type;
                                typedef boost::shared_ptr<const r_type>                   ptr_const_r_type;

                        private:
                                ptr_const_r_type ptr_const_r;
                                double _x;
                };
                void M_x_fc::update(double x_){
                        _x = x_;
                };
                double M_x_fc::mean()const{return ptr_const_r->mean();};

                class M_y_fc{
                        public:
                                typedef M_x_fc                                  l_type;
                                typedef boost::shared_ptr<const l_type>           ptr_const_l_type;
                                void set(ptr_const_l_type ptr_l_compl);
                                double mean()const;
                        private:

                                double          _rho;
                                double          _mean;
                                ptr_const_l_type ptr_l_compl;
                };
                void M_y_fc::update(){
                        _mean = _rho*(ptr_l_compl->x());
                };

                double M_y_fc::mean()const{return _mean;};

                class Config_fcfc{
                        public:
                                typedef M_x_fc                                                          l_fc_type;
                                typedef M_y_fc                                                          r_fc_type;
                                typedef boost::shared_ptr<l_fc_type>                                      ptr_l_fc_type;
                                typedef boost::shared_ptr<r_fc_type>                                      ptr_r_fc_type;
                                Config_fcfc(double rho);

                                ptr_r_fc_type   ptr_r_fc_0;
                                ptr_r_fc_type   ptr_r_fc_1;
                                ptr_l_fc_type   ptr_l_fc_0;
                                ptr_l_fc_type   ptr_l_fc_1;
                };

                Config_fcfc::Config_fcfc(double rho):
                        ptr_r_fc_0( new r_fc_type(0,rho)),
                        ptr_r_fc_1( new r_fc_type(1,rho)),
                        ptr_l_fc_0( new l_fc_type(ptr_r_fc_0)),
                        ptr_l_fc_1( new l_fc_type(ptr_r_fc_1))
                {
                        ptr_r_fc_0->set(ptr_l_fc_1);
                        ptr_r_fc_1->set(ptr_l_fc_0);
                };- Hide quoted text -

- Show quoted text -

ps: i'm aware that this problem can be solved by breaking dependencies
using inheritance but i'm interested in a fully generic solution.
knowing if that generic solution does not exist would already help.
 
V

Victor Bazarov

er said:
What I already have is a specific configuration {(M_x_fc,M_y_fc),
(M_x_fc,M_y_fc)} (see below). What i'd like to able to do is have an
arbitrary configuration, so long as the required operations are
supported. For example, M_x_fc requires that r_type support mean(),
and M_y_fc requires that l_type support x(); Please ask if still not
clear.


class M_x_fc{
public:
void update(double);
double x()const;
typedef M_y_fc r_type;
typedef boost::shared_ptr<const r_type> ptr_const_r_type;

private:
ptr_const_r_type ptr_const_r;
double _x;
};
void M_x_fc::update(double x_){
_x = x_;
};
double M_x_fc::mean()const{return ptr_const_r->mean();};


class M_y_fc{
public:
typedef M_x_fc l_type;
typedef boost::shared_ptr<const l_type> ptr_const_l_type;
void set(ptr_const_l_type ptr_l_compl);
double mean()const;
private:

double _rho;
double _mean;
ptr_const_l_type ptr_l_compl;
};
void M_y_fc::update(){
_mean = _rho*(ptr_l_compl->x());
};

double M_y_fc::mean()const{return _mean;};


class Config_fcfc{
public:
typedef M_x_fc l_fc_type;
typedef M_y_fc r_fc_type;
typedef boost::shared_ptr<l_fc_type> ptr_l_fc_type;
typedef boost::shared_ptr<r_fc_type> ptr_r_fc_type;
Config_fcfc(double rho);

ptr_r_fc_type ptr_r_fc_0;
ptr_r_fc_type ptr_r_fc_1;
ptr_l_fc_type ptr_l_fc_0;
ptr_l_fc_type ptr_l_fc_1;
};

Config_fcfc::Config_fcfc(double rho):
ptr_r_fc_0( new r_fc_type(0,rho)),
ptr_r_fc_1( new r_fc_type(1,rho)),
ptr_l_fc_0( new l_fc_type(ptr_r_fc_0)),
ptr_l_fc_1( new l_fc_type(ptr_r_fc_1))
{
ptr_r_fc_0->set(ptr_l_fc_1);
ptr_r_fc_1->set(ptr_l_fc_0);
};

It's still unclear what exactly you need to do and can't.

template<class T> class M_x;
template<class T> class M_y;

template<class T> class M_x {
typedef M_y<T> r_type;
...
};

template<class T> class M_x {
typedef M_x<T> l_type;
...
};

Make sure you define all members _after_ both class templates
are defined, and not after _each_ class template.

V
 

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,982
Messages
2,570,186
Members
46,744
Latest member
CortneyMcK

Latest Threads

Top