473,379 Members | 1,170 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

static virtual

I know that you can't have a static virtual property, but is there a
way to simulate the same results?

I have a base class that I want to extend so that you can change a
value and it inherits all of the methods to work with that value.
That's easy enough:

class BaseC {
string val;

public DoSomething() {
//manipulate val
}
}

class Derived : BaseC {
public Derived() {
val = "foo"
}
}

Derived.DoSomething() now manipulates "foo"

Now if I want BaseC to have static methods, that also manipulate
'val', I have to make val static. But when I have multiple derived
classes with static constructors, the static variable is for all of
the derived classes (bad).

I thought I could make static virtual properties that could just be
overloaded in the derived classes to return the correct value, but as
I stated, static virtual is not allowed.

Any Ideas?
Mark
Nov 16 '05 #1
2 3327
Try using the singleton design pattern. Make the Base a singleton. When
derived types get constructed, a single base is created for that derv
instance exclusively. This derv instance could manipulate the variable
originally defined in Base, and would not meddle with other derv's definition
as well.

"Mark" wrote:
I know that you can't have a static virtual property, but is there a
way to simulate the same results?

I have a base class that I want to extend so that you can change a
value and it inherits all of the methods to work with that value.
That's easy enough:

class BaseC {
string val;

public DoSomething() {
//manipulate val
}
}

class Derived : BaseC {
public Derived() {
val = "foo"
}
}

Derived.DoSomething() now manipulates "foo"

Now if I want BaseC to have static methods, that also manipulate
'val', I have to make val static. But when I have multiple derived
classes with static constructors, the static variable is for all of
the derived classes (bad).

I thought I could make static virtual properties that could just be
overloaded in the derived classes to return the correct value, but as
I stated, static virtual is not allowed.

Any Ideas?
Mark

Nov 16 '05 #2
I don't see how static virtual makes any sense.
How do you expect to manipulate instance variables of
a class from static methods of the same class?!?!
RayO
"Mark" <ma*****@rit.edu> wrote in message
news:d4**************************@posting.google.c om...
I know that you can't have a static virtual property, but is there a
way to simulate the same results?

I have a base class that I want to extend so that you can change a
value and it inherits all of the methods to work with that value.
That's easy enough:

class BaseC {
string val;

public DoSomething() {
//manipulate val
}
}

class Derived : BaseC {
public Derived() {
val = "foo"
}
}

Derived.DoSomething() now manipulates "foo"

Now if I want BaseC to have static methods, that also manipulate
'val', I have to make val static. But when I have multiple derived
classes with static constructors, the static variable is for all of
the derived classes (bad).

I thought I could make static virtual properties that could just be
overloaded in the derived classes to return the correct value, but as
I stated, static virtual is not allowed.

Any Ideas?
Mark

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: john smith | last post by:
I'm wondering if it's possible to declare a pure virtual member function? Ie is: class A{ public: virtual static void f() const = 0; }; legal? I'm getting compile errors for code that used...
3
by: Philippe Guglielmetti | last post by:
Look at these few lines of code: class A { public: virtual void f() { cout << "A";}}; class B : public A{public: static void f() { cout << "B"; }}; class C : public B{public: void f() { cout <<...
7
by: qazmlp | last post by:
What do you recommend among the following two designs, considering all the standard design aspects ? 1) class base { public: virtual ~base() { } virtual long int getId() = 0; private:
6
by: Dumitru Sipos | last post by:
Hello everybody! is there possible to have a function that is both static and virtual? Dumi.
5
by: Tony Johansson | last post by:
Hello experts! Why is not possible to have virtual static members Many thnakn //Tony
15
by: Philipp | last post by:
Hello I don't exactly understand why there are no static virtual functions. I would have liked something like this: class Base{ static virtual std::string getName(){ return "Base"; } }
1
by: Sandro Bosio | last post by:
Hello everybody, my first message on this forum. I tried to solve my issue by reading other similar posts, but I didn't succeed. And forgive me if this mail is so long. I'm trying to achieve the...
8
by: crjjrc | last post by:
Hi, I've got a base class and some derived classes that look something like this: class Base { public: int getType() { return type; } private: static const int type = 0; };
6
by: Jia | last post by:
Hi all, I have a class foo which has a static vector of pointers of type base class, and a static function to set this vector. #include <iostream> #include <vector> using namespace std;...
2
by: cmonthenet | last post by:
Hello, I searched for an answer to my question and found similar posts, but none that quite addressed the issue I am trying to resolve. Essentially, it seems like I need something like a virtual...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.