🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

1 total messages Started by Jean-Louis Moser Wed, 18 Oct 1995 18:10
adding template properties
#2243
Author: Jean-Louis Moser
Date: Wed, 18 Oct 1995 18:10
60 lines
2456 bytes
| QUESTION 1:
| why doesn't c++ accept the template function
| has_property (const Property<Obj>&) as a specialization of
| has_property (const Obj&). (idem for set_property).

| QUESTION 2:
| According to c++ standard, is there a way to define global functions
| has_property and set_property that enable to write for any object:

       obj.has_property();

| that would enable to define these as Property's methods.

| this programming approch intents to design simple objects then
| add to these objects template properties (ex: color, sharability
| and so on). An object with some properties has the general
| functionalities of the base class and some additional
| functionalities associated with its properties.

#include <iostream.h>

enum boolean {false, true};

class MyObj {
    unsigned i_;
public:
    MyObj (unsigned i) : i_ (i) {}
};

template <class Obj> class Property : public Obj {
    unsigned	prop_;
public:
    Property (const Obj& o)			: Obj (o), prop_ (0) {}
    Property (const Obj& o, unsigned prop)	: Obj (o), prop_ (prop) {}
    void set_property (unsigned p) {prop_ = p;}
};

template <class Obj> boolean has_property (const Property<Obj>&) {return true;}
template <class Obj> boolean has_property (const Obj&) {return false;}
template <class Obj> void    set_property (Property<Obj>& o, int p) {o.set_property(p);}
template <class Obj> void    set_property (Obj&, int) {throw "don't have property";}

void main ()
{
    MyObj o1 (10);
    Property<MyObj> o2 (20);
    cout << has_property (o1) << "," << has_property(o2) << endl;
    set_property (o1,1);
    set_property (o2,1);
}

thank you to enlight me. regards.

jlm


[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads