 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Eric W Guest
|
Posted: Tue Sep 16, 2003 6:38 am Post subject: static and virtual |
|
|
Hello,
Below is an outline of what I would like to be able to do. I am
referring to A as some base class and B as a derived class.
int main(int argc, char* argv[])
{
std::string str = GetName();
// i would like to be able to do something similar to this
// (access B's public static function)
if (str == B::name())
A* pA = new B;
// then later on i'd like to display the name
// (print out the name using only a pointer to A)
std::cout << "name is " << pA->name() << std::endl;
}
In the above code snipet the name() function is the issue. It should
be static in the derived class B, because all B's share the same name.
But, it needs to be virutal in class A, because I would like to be
able to access B's name through a pointer to A.
Since virtual functions cannot be static, I haven't been able to
figure out how to get a clean solution to this problem.
Any help would be appreicated. Thanks a lot!
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Tue Sep 16, 2003 1:20 pm Post subject: Re: static and virtual |
|
|
In article <2d693c65.0309151303.13d7d0fd (AT) posting (DOT) google.com>, Eric W
<etwimmer (AT) hotmail (DOT) com> writes
| Quote: | Hello,
Below is an outline of what I would like to be able to do. I am
referring to A as some base class and B as a derived class.
int main(int argc, char* argv[])
{
std::string str = GetName();
// i would like to be able to do something similar to this
// (access B's public static function)
if (str == B::name())
A* pA = new B;
// then later on i'd like to display the name
// (print out the name using only a pointer to A)
std::cout << "name is " << pA->name() << std::endl;
}
In the above code snipet the name() function is the issue. It should
be static in the derived class B, because all B's share the same name.
But, it needs to be virutal in class A, because I would like to be
able to access B's name through a pointer to A.
Since virtual functions cannot be static, I haven't been able to
figure out how to get a clean solution to this problem.
Any help would be appreicated. Thanks a lot!
|
I think you are trying to address the wrong problem. Placing the
following virtual member function in each derived class would meet your
specified need:
std::string const & name() const{
static std::string myname(/* whatever */);
return myname;
}
However the problem is that C++ does not provide a mechanism by which
you can require a virtual function be overridden in every derived class.
Not that that has nothing to do with providing static virtual.
Note that the concept of a static member is that of providing class
properties as opposed to object ones. Providing virtual static members
would entirely change the C++ class model effectively making classes
meta-objects.
--
Francis Glassborow ACCU
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Risto Lankinen Guest
|
Posted: Tue Sep 16, 2003 8:35 pm Post subject: Re: static and virtual |
|
|
Hi!
"Francis Glassborow" <francis (AT) robinton (DOT) demon.co.uk> wrote
| Quote: |
I think you are trying to address the wrong problem. Placing the
following virtual member function in each derived class would meet your
specified need:
std::string const & name() const{
static std::string myname(/* whatever */);
return myname;
}
|
That is not true, since the original poster wants this...
Base *p = new Derived;
p->myname();
..... to invoke Derived::myname(), whereas using your suggested
solution will invoke Base::myname() .
| Quote: | However the problem is that C++ does not provide a mechanism by which
you can require a virtual function be overridden in every derived class.
Not that that has nothing to do with providing static virtual.
|
This is a separate concept, although it would also be useful if
it was available in C++. Pure virtuals come close, but not
close enough. :-)
| Quote: | Note that the concept of a static member is that of providing class
properties as opposed to object ones. Providing virtual static members
would entirely change the C++ class model effectively making classes
meta-objects.
|
That is exactly the point, except that the object model would
not need to be changed much in the perspective of the most
typical virtual function invocation mechanism (i.e. that of using
vtables to store pointers to virtual member functions). It would
be very easy to pluck the address of a static function into the
vtbl, and the compiler would have all necessary meta-information
of the class available at run-time to know that no "this" pointer
shall be passed even though the method is virtual.
Cheers!
- Risto -
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
JarlOstensen Guest
|
Posted: Tue Sep 16, 2003 8:38 pm Post subject: Re: static and virtual |
|
|
[email]etwimmer (AT) hotmail (DOT) com[/email] (Eric W) wrote in message news:<2d693c65.0309151303.13d7d0fd (AT) posting (DOT) google.com>...
| Quote: | ...snip...
In the above code snipet the name() function is the issue. It should
be static in the derived class B, because all B's share the same name.
But, it needs to be virutal in class A, because I would like to be
able to access B's name through a pointer to A.
Since virtual functions cannot be static, I haven't been able to
figure out how to get a clean solution to this problem.
Any help would be appreicated. Thanks a lot!
|
Hi,
What you _could_ do is to make the Name method virtual, and B's
implementation share a static variable, (in B), i.e.
struct BaseInterface
{
...
virtual const char* Name() = 0;
};
....
class A
: public BaseInterface
{
...
};
....
class B
: public A
{
...
const char * Name()
{
return sName_;
}
...
private:
static const char* sName_;
};
....
const char * B::sName_ = "B";
Not quite what you want, I know, but pretty much the same behaviour
bar the extra vtbl entry for Name()....
-=jarl
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Wed Sep 17, 2003 9:07 am Post subject: Re: static and virtual |
|
|
In article <EuE9b.2546$g4.53885 (AT) news2 (DOT) nokia.com>, Risto Lankinen
<rlankine (AT) hotmail (DOT) com> writes
| Quote: | Hi!
"Francis Glassborow" <francis (AT) robinton (DOT) demon.co.uk> wrote in message
news:F$mE7rCenrZ$Ewe3 (AT) robinton (DOT) demon.co.uk...
I think you are trying to address the wrong problem. Placing the
following virtual member function in each derived class would meet your
specified need:
std::string const & name() const{
static std::string myname(/* whatever */);
return myname;
}
That is not true, since the original poster wants this...
Base *p = new Derived;
p->myname();
.... to invoke Derived::myname(), whereas using your suggested
solution will invoke Base::myname() .
|
A call to a virtual function through a pointer uses the dynamic type of
the pointer not the static type when the function being called is
designated as virtual (and if you read my words, you will see that is
the case I am dealing with)
--
Francis Glassborow ACCU
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
White Wolf Guest
|
Posted: Wed Sep 17, 2003 9:42 am Post subject: Re: static and virtual |
|
|
Francis Glassborow wrote:
[SNIPVS MAXIMVS]
| Quote: | However the problem is that C++ does not provide a mechanism by which
you can require a virtual function be overridden in every derived
class. Not that that has nothing to do with providing static virtual.
Note that the concept of a static member is that of providing class
properties as opposed to object ones. Providing virtual static members
would entirely change the C++ class model effectively making classes
meta-objects.
|
However. Khm. Should I? May I? What will they say when we meet?...
A book of mine says that the stars do not determine and they do not
inclinate (I know, there is no such word in English) but they are just a way
to see the quality of time as we look at the watch for the amount of time...
This last few days seems to be the "time for static virtual".
When you look at it this way (as I and Francis did) "static virtual" sounds
like "unchanging dynamic" or "alcohol-free vodka". However there is another
approach to the question. Forget about the subject and the wording of the
problem statement and look at the problem! What is the real problem?
The real wish is: that people need a way to get information about a type,
information which they defined. Also they want it to be possible to get
this information for a type or for the type of an expression. Let's say it
again, shorter: we want to get information from a type or from the type of
an expression. Rings a bell? Yes, it is typeid in all of its glory. At
least it seems so to me.
So what is it all together then? It seems that people could use a facility
where typeinfo could be extended by user provided information.
(Basically we would like to be able to place "things" into the virtual
function table of a class. That is accessible if a type is known as well as
when we have an object. For non-polymorph types this can be static
information accessed from the type. I feel that this whole idea could go
best together with Daveed's language extensions, since those "functions" we
put into the virtual table (via the typeid) should be run compile time!)
--
WW aka Attila
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Jon Heiner Guest
|
Posted: Wed Sep 17, 2003 9:47 am Post subject: Re: static and virtual |
|
|
rather than critique your code, because it looks like you are
doing some rather "funny" stuff with OO, here, i'll just give
you a quick and dirty workaround. just wrap a call to a
static member with a virtual function like so. not sure what
you're trying to do exactly here, but this at least achieves
your immediate goal. -jon
//--------------------------------------------------------------------------
------
class A {
public:
virtual const std::string& getName( void ) const;
};
class B {
public:
static const std::string& getClassName( void ) const;
virtual const std::string& getName( void ) const { return
B::getClassName(); }
};
//--------------------------------------------------------------------------
------
"Eric W" <etwimmer (AT) hotmail (DOT) com> wrote
| Quote: | Hello,
Below is an outline of what I would like to be able to do. I am
referring to A as some base class and B as a derived class.
int main(int argc, char* argv[])
{
std::string str = GetName();
// i would like to be able to do something similar to this
// (access B's public static function)
if (str == B::name())
A* pA = new B;
// then later on i'd like to display the name
// (print out the name using only a pointer to A)
std::cout << "name is " << pA->name() << std::endl;
}
In the above code snipet the name() function is the issue. It should
be static in the derived class B, because all B's share the same name.
But, it needs to be virutal in class A, because I would like to be
able to access B's name through a pointer to A.
Since virtual functions cannot be static, I haven't been able to
figure out how to get a clean solution to this problem.
|
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Eric W Guest
|
Posted: Wed Sep 17, 2003 10:15 am Post subject: Re: static and virtual |
|
|
| Quote: | I think you are trying to address the wrong problem. Placing the
following virtual member function in each derived class would meet your
specified need:
std::string const & name() const{
static std::string myname(/* whatever */);
return myname;
}
However the problem is that C++ does not provide a mechanism by which
you can require a virtual function be overridden in every derived class.
Not that that has nothing to do with providing static virtual.
Note that the concept of a static member is that of providing class
properties as opposed to object ones. Providing virtual static members
would entirely change the C++ class model effectively making classes
meta-objects.
|
Thanks Francis.
I have figured out a way around my particular problem, using something
similar to what you suggested. However, I am still kind of bothered
by my solution, because I think there must be a way to get the best of
both worlds (static and virtual), and I am just missing it.
For example, I thought of an application that might be of use at a
place like SeaWorld, where they might have several Orca's in
captivity. Say they have 2 killer whales in their tanks. Each Orcaa
has his own name given to it by the trainers (e.g. Shamoo). In
additon, all killer whales come from the same order, Ceteacea. There
is a static string value for the animal's order and a virtual function
for to get the Animal's name. But, it would be nice to be able to get
the Animal's order through the base class as well:
// note - this won't compile but this is the intent of what I was
aiming for
class Animal {
public:
virtual const string& order() = 0;
virutal const string& name() = 0;
};
class Orca : public Animal {
public:
Orca(const string& name) : name_(name) {}
static const string& order() { return order_; }
const string& name() { return name_; }
private:
static const string order_;
string name_;
};
const string Orca::order_ = "Ceteacea";
int main(int argc, char* argv[])
{
string name, order;
// get the name and order from somewhere
// could be some abstract factory to create SeaWorld animals from a
database
if (order == Orca::order()) {
Animal* pAnimal = new Orca(name);
....
// now later on I want to print out the order of the animal pointed
to by
// pAnimal
cout << "The order of this animal is " << pAnimal->order() << endl;
delete pAnimal;
}
This is probaly not the best example, because you could argue that
Order could be its own class, and Ceteacea would be derived from it,
and would contain properties global to all Ceteacea's. But,
forgetting about that, is there no way to get the behavior I have
lined out? I might be trying to compare apples and oranges here.
Also, you stated that C++ has no mechanism by which you can require a
virtual function be overridden in every derived class, but isn't that
the purpose of the pure virtual specifier?
Interested in your feedback. Thanks again.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Risto Lankinen Guest
|
Posted: Wed Sep 17, 2003 8:00 pm Post subject: Re: static and virtual |
|
|
Hi!
"Francis Glassborow" <francis (AT) robinton (DOT) demon.co.uk> wrote
| Quote: | In article <EuE9b.2546$g4.53885 (AT) news2 (DOT) nokia.com>, Risto Lankinen
[email]rlankine (AT) hotmail (DOT) com[/email]> writes
std::string const & name() const{
static std::string myname(/* whatever */);
return myname;
}
That is not true, since the original poster wants this...
Base *p = new Derived;
p->myname();
.... to invoke Derived::myname(), whereas using your suggested
solution will invoke Base::myname() .
A call to a virtual function through a pointer uses the dynamic type of
the pointer not the static type when the function being called is
designated as virtual (and if you read my words, you will see that is
the case I am dealing with)
|
Having reread your example I realize I was mistaking
the variable declaration "static std::string myname()" as
a declaration of a static member function. Anyway, my
point remains, but the argument now changes:
The original poster also wants this...
Base::name();
..... or this...
Derived::name();
..... to be invokeable without having an actual instance
of a Base object.
This is something you can't do using the currently
existing virtual function model without instantiating
an object or (quite dangerously!) pretending to
have one by type casting a null pointer, as below:
((Base *)0)->name(); // <-- Don't do! (Illustration only)
As with some other proposals (such as "final" methods
and/or classes) there should be nothing subjective here.
Francis, knowing your track record in the development
of the C++ standard, it surprises me that you don't seem
to recognize the usefulness of this proposal. Having said
that, it is not meant to disrespect you but to motivate into
rethinking outside of the box ;-)
Cheers!
- Risto -
P.S. One extra benefit of static virtual functions has not
been mentioned in this thread yet: The pointer to such a
function could be made compatible with plain C function
pointers (as opposed to member pointers) so that they
can be passed as call back methods to an API based on
C. With the risk of violating the requirement for platform
neutrality in this forum, Windows is one example of such
an API which would benefit a lot if [C++] window classes
could register a polymorphic [C or static C++] window
procedure for each different [Windows OS] WndClass.
Static virtual mehods would seem to fit the bill perfectly.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Wed Sep 17, 2003 8:07 pm Post subject: Re: static and virtual |
|
|
In article <2d693c65.0309161343.519d3f45 (AT) posting (DOT) google.com>, Eric W
<etwimmer (AT) hotmail (DOT) com> writes
| Quote: | Also, you stated that C++ has no mechanism by which you can require a
virtual function be overridden in every derived class, but isn't that
the purpose of the pure virtual specifier?
|
No, it is often thought of that way but it only requires that the the
pure virtual be overridden at some stage once this is done classes that
derive from the class providing an overrider have no requirement to
provide one themselves.
--
Francis Glassborow ACCU
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Wed Sep 17, 2003 8:08 pm Post subject: Re: static and virtual |
|
|
In article <bk7ln4$k4s$1 (AT) phys-news1 (DOT) kolumbus.fi>, White Wolf
<wolof (AT) freemail (DOT) hu> writes
| Quote: | When you look at it this way (as I and Francis did) "static virtual" sounds
like "unchanging dynamic" or "alcohol-free vodka". However there is another
approach to the question. Forget about the subject and the wording of the
problem statement and look at the problem! What is the real problem?
The real wish is: that people need a way to get information about a type,
information which they defined. Also they want it to be possible to get
this information for a type or for the type of an expression. Let's say it
again, shorter: we want to get information from a type or from the type of
an expression. Rings a bell? Yes, it is typeid in all of its glory. At
least it seems so to me.
|
Exactly, another example of a problem masquerading as a solution.
Reflection is certainly something we will consider for the next C++
standard but we will approach it with an open mind as to what are the
appropriate solutions.
--
Francis Glassborow ACCU
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Wed Sep 17, 2003 8:12 pm Post subject: Re: static and virtual |
|
|
"Risto Lankinen" <rlankine (AT) hotmail (DOT) com> wrote
| Quote: | "Francis Glassborow" <francis (AT) robinton (DOT) demon.co.uk> wrote in message
news:F$mE7rCenrZ$Ewe3 (AT) robinton (DOT) demon.co.uk...
I think you are trying to address the wrong problem. Placing the
following virtual member function in each derived class would meet
your specified need:
std::string const & name() const{
static std::string myname(/* whatever */);
return myname;
}
That is not true, since the original poster wants this...
Base *p = new Derived;
p->myname();
.... to invoke Derived::myname(), whereas using your suggested
solution will invoke Base::myname() .
|
He specifically said a *virtual* member function. So in the above,
p->myname() will invoke Derived::myname.
| Quote: | However the problem is that C++ does not provide a mechanism by
which you can require a virtual function be overridden in every
derived class. Not that that has nothing to do with providing static
virtual.
This is a separate concept, although it would also be useful if it was
available in C++. Pure virtuals come close, but not close enough.
|
About the only way I can think of to enforce this (and then only via a
runtime check) is to maintain a map, and verify that no two different
types ever return the same value, along the lines of :
std::string
Object::className() const
{
std::string result = doClassName() ;
typedef std::map< std::string, std::type_info const* >
NameMap ;
static NameMap names ;
assert( names.insert( NameMap::value_type( result, &typeid( *this ) ) )
.first->second == &typeid( *this ) ) ;
return result ;
}
(Note that this will also catch the case in which you inadvertently
choose the same name for two different classes.)
| Quote: | Note that the concept of a static member is that of providing class
properties as opposed to object ones. Providing virtual static
members would entirely change the C++ class model effectively making
classes meta-objects.
That is exactly the point, except that the object model would not need
to be changed much in the perspective of the most typical virtual
function invocation mechanism (i.e. that of using vtables to store
pointers to virtual member functions). It would be very easy to pluck
the address of a static function into the vtbl, and the compiler would
have all necessary meta-information of the class available at run-time
to know that no "this" pointer shall be passed even though the method
is virtual.
|
And what remains of the "static"? The whole point of a static function
is that it isn't associated with any particular object, and can be
called without an object. Since the virtual function mechanism requires
an object, there is no point in a virtual function being static.
With regards to considering classes as meta-objects, I believe that
there is some research underway to add additional information to the
type_info object. This information might possibly also provide some way
of calling a static function in the class through the type_info. (On
the other hand, I had this possibility in Java, and about the only use I
found for it was test classes. In this particular case, it would be
sufficient to specify something reasonable as the required result of
type_info::name.)
--
James Kanze GABI Software mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/ http://www.gabi-soft.fr
Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France, +33 (0)1 30 23 45 16
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Jon Heiner Guest
|
Posted: Wed Sep 17, 2003 8:34 pm Post subject: Re: static and virtual |
|
|
Eric- you are so close. you seem to be blending two core C++
concepts: virtual functions with static/non-static members which
have separate motivations.
static member variables are shared between all instances
of a class. non-static member variables are stored separately for
all instances of a class. same with static and non-static member
functions.
what you want is to represent this distinction appropriately. so each
instance of a class/object has its own name ( member variable/function ).
whereas each class of objects shares an order ( static member variable/
function. )
you are getting confused with virtual member functions which have
nothing to do with static member functions. the purpose of virtual
member functions is to provide a consistent interface to a hierarchy
of objects. to represent this, the base class defines an interface that
each derived class must override appropriately.
the code below shows these two separations. the Animal base class
has a clean interface of ( pure ) virtual functions. the Orca derived
class overrides these functions in order to provide specialization.
separately, the data that is shared between all instances of a class
( in this case the Orca's order ) is wrapped in a static function.
just because the virtual order() method uses the static data of
the class does not mean there is such a thing as static virtual.
hope this helps! -jon
----------------------------------------------------------------
class Animal {
public:
virtual const string& order() = 0;
virtual const string& name() = 0;
};
class Orca : public Animal {
public:
Orca(const string& name) : name_(name) {}
virtual const string& order() { return getOrder(); }
virtual const string& name() { return name_; }
private:
static const string& getOrder()
{
static firstTime = true;
if( !firstTime ) return Orca::order_;
Orca::order_ = "Ceteacea"; // or put this in a manager/init
somewhere.
firstTime = false;
}
static string order_;
string name_;
};
void Database::AddAnimal( const string& name, const string& order )
{
if( order != Orca::order() )
return;
Animal* pAnimal = new Orca(name);
cout << "Animal " << pAnimal->name() << " of type " << pAnimal->order()
<< " added." << endl;
}
"Eric W"
| Quote: | I think you are trying to address the wrong problem. Placing the
following virtual member function in each derived class would meet your
specified need:
std::string const & name() const{
static std::string myname(/* whatever */);
return myname;
}
However the problem is that C++ does not provide a mechanism by which
you can require a virtual function be overridden in every derived
class.
Not that that has nothing to do with providing static virtual.
Note that the concept of a static member is that of providing class
properties as opposed to object ones. Providing virtual static members
would entirely change the C++ class model effectively making classes
meta-objects.
Thanks Francis.
I have figured out a way around my particular problem, using something
similar to what you suggested. However, I am still kind of bothered
by my solution, because I think there must be a way to get the best of
both worlds (static and virtual), and I am just missing it.
For example, I thought of an application that might be of use at a
place like SeaWorld, where they might have several Orca's in
captivity. Say they have 2 killer whales in their tanks. Each Orcaa
has his own name given to it by the trainers (e.g. Shamoo). In
additon, all killer whales come from the same order, Ceteacea. There
is a static string value for the animal's order and a virtual function
for to get the Animal's name. But, it would be nice to be able to get
the Animal's order through the base class as well:
// note - this won't compile but this is the intent of what I was
aiming for
class Animal {
public:
virtual const string& order() = 0;
virutal const string& name() = 0;
};
class Orca : public Animal {
public:
Orca(const string& name) : name_(name) {}
static const string& order() { return order_; }
const string& name() { return name_; }
private:
static const string order_;
string name_;
};
const string Orca::order_ = "Ceteacea";
int main(int argc, char* argv[])
{
string name, order;
// get the name and order from somewhere
// could be some abstract factory to create SeaWorld animals from a
database
if (order == Orca::order()) {
Animal* pAnimal = new Orca(name);
...
// now later on I want to print out the order of the animal pointed
to by
// pAnimal
cout << "The order of this animal is " << pAnimal->order() << endl;
delete pAnimal;
}
This is probaly not the best example, because you could argue that
Order could be its own class, and Ceteacea would be derived from it,
and would contain properties global to all Ceteacea's. But,
forgetting about that, is there no way to get the behavior I have
lined out? I might be trying to compare apples and oranges here.
Also, you stated that C++ has no mechanism by which you can require a
virtual function be overridden in every derived class, but isn't that
the purpose of the pure virtual specifier?
Interested in your feedback. Thanks again.
|
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
White Wolf Guest
|
Posted: Thu Sep 18, 2003 9:47 am Post subject: Re: static and virtual |
|
|
Francis Glassborow wrote:
| Quote: | In article <bk7ln4$k4s$1 (AT) phys-news1 (DOT) kolumbus.fi>, White Wolf
[email]wolof (AT) freemail (DOT) hu[/email]> writes
When you look at it this way (as I and Francis did) "static virtual"
sounds like "unchanging dynamic" or "alcohol-free vodka". However
there is another approach to the question. Forget about the subject
and the wording of the problem statement and look at the problem!
What is the real problem?
The real wish is: that people need a way to get information about a
type, information which they defined. Also they want it to be
possible to get this information for a type or for the type of an
expression. Let's say it again, shorter: we want to get information
from a type or from the type of an expression. Rings a bell? Yes,
it is typeid in all of its glory. At least it seems so to me.
Exactly, another example of a problem masquerading as a solution.
|
Problem? What do you mean? An idea to get more access to the types -
without switching to Java and using existing language features which are
cheap to extend - is a problem? I rather see it as a step towards the
solution.
| Quote: | Reflection is certainly something we will consider for the next C++
standard but we will approach it with an open mind as to what are the
appropriate solutions.
|
Last time I saw reflection it was on a mirror. So I cannot be sure what do
you mean by it... but. AFAIK it is about looking into a type. That is all
right. But what if I want to "attach" something more to that type? To the
type, not to the instances of it.
I was giving a route to go. I see no error in my reasoning above and I
cannot see any attempt in your answer to prove it wrong. You have simply
stated that it is a problem and not a solution and moved on dismissing the
idea so fast, that it starts to make the reader believe that you have never
really looked at it at all.
I have given a whole path of thinking coming to a conclusion. You have
given a judgement/ruling without any rationale. Then completely dismissed
the idea (any further discussion of it) with the /we are working on it (in
long from)/, which is another way of saying /don't call us, we will call
you/ or the Hungarian version of it: /talk into my pocket, I will listen to
it at home/.
I really think that - even if I don't - the readers here deserve an
explanation. I gave mine. Please post yours.
--
WW aka Attila
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Eric W Guest
|
Posted: Thu Sep 18, 2003 9:59 am Post subject: Re: static and virtual |
|
|
| Quote: | class Animal {
public:
virtual const string& order() = 0;
virtual const string& name() = 0;
};
class Orca : public Animal {
public:
Orca(const string& name) : name_(name) {}
virtual const string& order() { return getOrder(); }
virtual const string& name() { return name_; }
private:
static const string& getOrder()
{
static firstTime = true;
if( !firstTime ) return Orca::order_;
Orca::order_ = "Ceteacea"; // or put this in a manager/init
somewhere.
firstTime = false;
}
static string order_;
string name_;
};
void Database::AddAnimal( const string& name, const string& order )
{
if( order != Orca::order() )
return;
Animal* pAnimal = new Orca(name);
cout << "Animal " << pAnimal->name() << " of type " << pAnimal->order()
" added." << endl;
}
|
Jon -
Thanks, this is exactly what I was trying to do. The one part that is
still unclear is the use of Orca::order() in your Database::AddAnimal
function. Since order is a virtual function and not a static
function, I didn't think it could be accessed via Orca::order() (even
though it accesses a static function, order itself is still a virtual
function).
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|