🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #2243

adding template properties

#2243
From: 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. ]
Technical Details
Message-ID: <JLM.95Oct18184717@ludo.two-oo-one.fr>
Path: arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!taumet!clamage

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!taumet!clamage
From: jlm@two-oo-one.fr (Jean-Louis Moser)
Newsgroups: comp.std.c++
Subject: adding template properties
Followup-To: comp.std.c++
Date: 18 Oct 1995 18:10:54 GMT
Organization: Images 2001 S.A
Approved: clamage@eng.sun.com (comp.std.c++)
Distribution: world
Message-ID: <JLM.95Oct18184717@ludo.two-oo-one.fr>
NNTP-Posting-Host: taumet.eng.sun.com
Content-Type: text
X-Nntp-Posting-Host: ludo.two-oo-one.fr
Content-Length: 1654
X-Lines: 56
Originator: clamage@taumet