473,769 Members | 2,249 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

recommended design for 'static const' member access

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:
static const long int BASE_ID = 10000 ;
};

long int base::getId()
{
return BASE_ID ;
}

class derived11 : public base
{
public:
virtual ~derived11()
{
}
virtual long int getId()
{
return base::getId() + ID ;
}

private:
static const long int ID = 11 ;
};

class derived12 : public base
{
public:
virtual ~derived12()
{
}
virtual long int getId()
{
return base::getId() + ID ;
}

private:
static const long int ID = 12 ;
};
2)
class base
{
public:
virtual long int getId() = 0;
protected:
static const long int BASE_ID = 10000 ;
};

class derived11 : public base
{
public:
virtual long int getId()
{
return BASE_ID + ID ;
}

private:
static const long int ID = 11 ;
};
class derived12 : public base
{
public:
virtual long int getId()
{
return BASE_ID + ID ;
}

private:
static const long int ID = 12 ;
};
Jul 22 '05 #1
7 1514
qazmlp wrote:
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:
static const long int BASE_ID = 10000 ;
};

long int base::getId()
{
return BASE_ID ;
}


You have coupled the concept of the base id with the concept of all the
derived id overrides. They are different. (To put this into "duplicatio n"
terms, all the derived getId() implementations duplicate each other.) Try
this:

class base
{
public:
virtual ~base() { }
long int getId() { return BASE_ID + getIdOffset(); }
private:
virtual long getIdOffset() = 0;
static const long int BASE_ID = 10000 ;
};

long base::getIdOffs et()
{
return 0;
}

long derived11::getI dOffset()
{
return 11;
}

long derived12::getI dOffset()
{
return 12;
}

That design exposes less virtual methods in base's public interface.

--
Phlip
http://www.xpsd.org/cgi-bin/wiki?Tes...UserInterfaces
Jul 22 '05 #2
The first is complicated and slow. The second is simple and fast. I
don't see any benefit to the polymorphism used in the first.
qa********@redi ffmail.com (qazmlp) might (or might not) have written
this on (or about) 31 Jan 2004 03:36:15 -0800, :
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:
static const long int BASE_ID = 10000 ;
};

long int base::getId()
{
return BASE_ID ;
}

class derived11 : public base
{
public:
virtual ~derived11()
{
}
virtual long int getId()
{
return base::getId() + ID ;
}

private:
static const long int ID = 11 ;
};

class derived12 : public base
{
public:
virtual ~derived12()
{
}
virtual long int getId()
{
return base::getId() + ID ;
}

private:
static const long int ID = 12 ;
};
2)
class base
{
public:
virtual long int getId() = 0;
protected:
static const long int BASE_ID = 10000 ;
};

class derived11 : public base
{
public:
virtual long int getId()
{
return BASE_ID + ID ;
}

private:
static const long int ID = 11 ;
};
class derived12 : public base
{
public:
virtual long int getId()
{
return BASE_ID + ID ;
}

private:
static const long int ID = 12 ;
};


Robert C. Martin | "Uncle Bob"
Object Mentor Inc. | unclebob @ objectmentor . com
501 N. Riverside Dr.| Tel: (800) 338-6716
Suite 206 | Fax: (847) 775-8174 | www.objectmentor.com
| | www.XProgramming.com
Gurnee, IL, | Training and Mentoring | www.junit.org
60031 | OO, XP, Agile, C++, Java, C# | http://fitnesse.org
Jul 22 '05 #3
qa********@redi ffmail.com (qazmlp) wrote in message news:<db******* *************** ****@posting.go ogle.com>...
What do you recommend among the following two designs, considering
all the standard design aspects ? .... virtual long int getId() = 0;

....

Your getId function is returning a different value for each type,
and the same value for all instances of the same type. In other
words, it is effectively a type identifier.

I have two questions, both concerning what the clients use
this facility for..

1) Won't the clients work just as well by asking an object for
its type? Isomorphic information will be returned.

2) Are the clients treating a variable in different ways
according to its returned getId() value? If so, please consider
using polymorphism instead!
Jul 22 '05 #4
u.************* @objectmentor.c om (Uncle Bob (Robert C. Martin)) wrote
(abridged):
The first is complicated and slow. The second is simple and fast.
If the first base::getId() function is inlined, they could be the same
speed. Indeed, they could generate the same assembler.

I don't see any benefit to the polymorphism used in the first.


It's easier to make changes to the first. The second base class exposes
more of its representation to the derived classes. I don't know if that
matters in the example.

-- Dave Harris, Nottingham, UK
Jul 22 '05 #5
On Tue, 3 Feb 2004 19:40 +0000 (GMT Standard Time), br******@cix.co .uk
(Dave Harris) wrote:
u.************ *@objectmentor. com (Uncle Bob (Robert C. Martin)) wrote
(abridged):
The first is complicated and slow. The second is simple and fast.


If the first base::getId() function is inlined, they could be the same
speed. Indeed, they could generate the same assembler.


The first base::getId() is a pure virtual function. I didn't think
you could inline that.
Jul 22 '05 #6
un******@object mentor.com (Robert C. Martin) wrote (abridged):
If the first base::getId() function is inlined, they could be the same
speed. Indeed, they could generate the same assembler.


The first base::getId() is a pure virtual function. I didn't think
you could inline that.


You can. If it is called with the base::getId() syntax, there's no virtual
dispatch involved, so the compiler knows at compile-time exactly which
function body will be invoked, and if it knows the source it can inline
it.

-- Dave Harris, Nottingham, UK
Jul 22 '05 #7
On Wed, 4 Feb 2004 22:32 +0000 (GMT Standard Time), br******@cix.co .uk
(Dave Harris) wrote:
un******@objec tmentor.com (Robert C. Martin) wrote (abridged):
>If the first base::getId() function is inlined, they could be the same
>speed. Indeed, they could generate the same assembler.


The first base::getId() is a pure virtual function. I didn't think
you could inline that.


You can. If it is called with the base::getId() syntax, there's no virtual
dispatch involved, so the compiler knows at compile-time exactly which
function body will be invoked, and if it knows the source it can inline
it.


<duh>
Doh!
</duh>
Jul 22 '05 #8

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

Similar topics

11
4616
by: Roger Leigh | last post by:
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++ for Linux in 21 Days--I know there are better) states that "static member functions cannot access any non-static member variables". However, this doesn't seem entirely correct. It also doesn't mention whether static member functions can access protected and private member data and methods (and I couldn't spot this in the FAQ). I have a class row<Row> which derives from...
2
1765
by: trying_to_learn | last post by:
while seeing an example i was surprised to see that a const member function is allowed to change a static data member as shown below. I am trying to reason why.... and my guess is that static data members really dont belong to an object rather they belong to a class. however, isn't const a strict thing ? : where the compiler says "if u make this member function a const, i promise i wont let the function change the state of an object", yet...
5
1615
by: ma740988 | last post by:
Select parameters in a vendors API file is as follows: #ifdef __cplusplus typedef void (*VOIDFUNCPTR)(...); /* ptr to function returning void */ #else typedef void (*VOIDFUNCPTR)(); /* ptr to function returning void */ #endif /* __cplusplus */
9
514
by: Martin Vorbrodt | last post by:
I'm designing a Matrix class of 4x4 matrices used in computer graphics. Both OpenGL and Direct3D can take a pointer to array of 16 floats which represent the values in the matrix. OGL takes it in column order, D3D in row order. Therefore row = 2, col = 3 will result in different offset into the float array depending on the API. Here is my basic design test program: #include <iostream> using std::cout; using std::endl;
9
2824
by: Grizlyk | last post by:
Somebody have offered std colors to C++ in the msg here: http://groups.google.com/group/comp.lang.c++/browse_frm/thread/2e5bb3d36ece543b/1acf6cd7e3ebdbcd#1acf6cd7e3ebdbcd The main objection to the including "colors markup" is useless of the marks, the marks is unneccessary extra information. The most used way to make colored view of C++ programs is usage of classes of C++ language words (already included in C++): reserved words, user...
4
1771
by: nw | last post by:
Hi, I was wondering if someone would be able to give me some comments on the following class structure, it feels to me as if there must be a better way, but I'm unsure what it is, perhaps I should be using multiple inheritance? Basically I have a pure virtual class called Body, this contains a number of integration algorithms which are applied to the Body. Generally only one integration algorithm will be used with a
3
1681
by: mimi | last post by:
1.Instead of using macro, I would like to use static const variables in such situations. class Foo { public: static const int SOMEVALUEWITHFOO = 1; } int main() {
3
2051
by: tomPee | last post by:
Hi, I have the following problem: I am trying to make some sort of base class menu that i can then use to derive other menu's from. Those menu's should then be able to interact with each other. And, i have most of the idea figured out and I thought out how i want to do it. But when i started coding i found a slight... difficulty. It might be easy to overcome, but google let me down :( and my own imagination made one happy jump, but...
5
1723
by: Soumen | last post by:
For one of my project, I want to use auto_ptr. But I'm facing few issues. 1. Say I've Controller class. I don't want to allow user to create object of this class unless all arguments are valid. So I've made the constructor private and given one static method to create an object of this class. Since one of senior members in the team doesn't like throwing exception, I've made this static method to return NULL pointer when arguments are...
0
10223
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10000
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8879
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3968
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3571
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.