Connecting Tech Pros Worldwide Forums | Help | Site Map

recommended design for 'static const' member access

qazmlp
Guest
 
Posts: n/a
#1: Jul 22 '05
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 ;
};

Phlip
Guest
 
Posts: n/a
#2: Jul 22 '05

re: recommended design for 'static const' member access


qazmlp wrote:
[color=blue]
> 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 ;
> }[/color]

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 "duplication"
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::getIdOffset()
{
return 0;
}

long derived11::getIdOffset()
{
return 11;
}

long derived12::getIdOffset()
{
return 12;
}

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

--
Phlip
http://www.xpsd.org/cgi-bin/wiki?Tes...UserInterfaces


Uncle Bob (Robert C. Martin)
Guest
 
Posts: n/a
#3: Jul 22 '05

re: recommended design for 'static const' member access


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.


qazmlp1209@rediffmail.com (qazmlp) might (or might not) have written
this on (or about) 31 Jan 2004 03:36:15 -0800, :
[color=blue]
>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 ;
>};[/color]

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
Graham Perkins
Guest
 
Posts: n/a
#4: Jul 22 '05

re: recommended design for 'static const' member access


qazmlp1209@rediffmail.com (qazmlp) wrote in message news:<db9bbf31.0401310336.59231354@posting.google. com>...[color=blue]
> What do you recommend among the following two designs, considering
> all the standard design aspects ?[/color]
....[color=blue]
> virtual long int getId() = 0;[/color]
....

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!
Dave Harris
Guest
 
Posts: n/a
#5: Jul 22 '05

re: recommended design for 'static const' member access


u.n.c.l.e.b.o.b@objectmentor.com (Uncle Bob (Robert C. Martin)) wrote
(abridged):[color=blue]
> The first is complicated and slow. The second is simple and fast.[/color]

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

[color=blue]
> I don't see any benefit to the polymorphism used in the first.[/color]

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
Robert C. Martin
Guest
 
Posts: n/a
#6: Jul 22 '05

re: recommended design for 'static const' member access


On Tue, 3 Feb 2004 19:40 +0000 (GMT Standard Time), brangdon@cix.co.uk
(Dave Harris) wrote:
[color=blue]
>u.n.c.l.e.b.o.b@objectmentor.com (Uncle Bob (Robert C. Martin)) wrote
>(abridged):[color=green]
>> The first is complicated and slow. The second is simple and fast.[/color]
>
>If the first base::getId() function is inlined, they could be the same
>speed. Indeed, they could generate the same assembler.[/color]

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


Dave Harris
Guest
 
Posts: n/a
#7: Jul 22 '05

re: recommended design for 'static const' member access


unclebob@objectmentor.com (Robert C. Martin) wrote (abridged):[color=blue][color=green]
> >If the first base::getId() function is inlined, they could be the same
> >speed. Indeed, they could generate the same assembler.[/color]
>
> The first base::getId() is a pure virtual function. I didn't think
> you could inline that.[/color]

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
Robert C. Martin
Guest
 
Posts: n/a
#8: Jul 22 '05

re: recommended design for 'static const' member access


On Wed, 4 Feb 2004 22:32 +0000 (GMT Standard Time), brangdon@cix.co.uk
(Dave Harris) wrote:
[color=blue]
>unclebob@objectmentor.com (Robert C. Martin) wrote (abridged):[color=green][color=darkred]
>> >If the first base::getId() function is inlined, they could be the same
>> >speed. Indeed, they could generate the same assembler.[/color]
>>
>> The first base::getId() is a pure virtual function. I didn't think
>> you could inline that.[/color]
>
>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.[/color]

<duh>
Doh!
</duh>


Closed Thread