Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > member function names identical to class names

Reply
Thread Tools

member function names identical to class names

 
 
Ares Lagae
Guest
Posts: n/a
 
      09-23-2004
When adopting the coding style of the standard C++ library, you often run
into naming problems because class names are lower case, and member
functions do not have get/set prefixes.

For example:
- The class stringstream has a member function "string stringstream::str()".
It would be more logical to name it "string stringstream::string()".
- The class ios_base has a member function "locale ios_base::getloc()". It
would be more logical to name it "locale ios_base::locale()". Especially
because the "get" prefix is never used in the standard library for
inspectors.

Lets consider that problem (I do not want to discuss coding styles, or which
name is the best for a member function).

Consider the following code snippet:

class foo {};

class bar
{
public:
foo foo()
{
return foo_;
}
private:
foo foo_;
};

Obviously this does not compile. Also changing the order of the private and
public part does not help.

class bar
{
private:
foo foo_;
public:
foo foo()
{
return foo_;
}
};

Adding "class" solves the problem:

class bar
{
public:
class foo foo()
{
return foo_;
}
private:
class foo foo_;
};

Now suppose the class foo is a class template.

template <typename T>
class foo {};

The obvious solution does not work.

class bar
{
public:
class foo<int> foo()
{
return foo_;
}
private:
class foo<int> foo_;
};

Have I missed something or used an incorrect syntax ?

Amazingly, changing the order of the private and public part now does help.

class bar
{
private:
class foo<int> foo_;
public:
class foo<int> foo()
{
return foo_;
}
};

Is there somewhere more information available about issues like this ?

best regards,
Ares Lagae
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      09-23-2004
Ares Lagae wrote:
> When adopting the coding style of the standard C++ library, you often run
> into naming problems because class names are lower case, and member
> functions do not have get/set prefixes.


No, I don't run into issues like that, why do you think I do?

> [...]
> Is there somewhere more information available about issues like this ?


Well, the standard library naming convention never struck me an confusing,
and whatever convention we're following here at work is a convention and
is supposed to make sense to everybody to be adopted. So, no issues so
far.

As to your example

class foo {};

class bar {
public:
foo foo();
};

It's clear nonsense because class bar should simply have the operator foo
instead of oddly named function.

Victor
 
Reply With Quote
 
 
 
 
Ares Lagae
Guest
Posts: n/a
 
      09-23-2004
Victor Bazarov wrote:

> Ares Lagae wrote:
>> When adopting the coding style of the standard C++ library, you often run
>> into naming problems because class names are lower case, and member
>> functions do not have get/set prefixes.

>
> No, I don't run into issues like that, why do you think I do?


Suppose you have a class color, and a class pen. Adopting the coding style
of the standard C++ library, pen would have a member function "color
pen::color()", and you would have a problem. You can solve this problem
like the C++ standard library does: rename the member function, and
introduce a less logical name. I hope you recognize this situation is
identical to the the "locale ios_base::getloc()" example.

When using aggregation, sometimes the role name can be used. For example,
when you have a class person and a class account, you would introduce a
member function "person account::holder()" instead of "person
account:erson". However, there are many cases in which a role name is not
obvious (like in the pen/color example). Then you run into the problem I
describe.

I explicitely mentioned I did not want to discuss coding style, or wether
the names in the standard C++ library are logical or not...

>
> As to your example
>
> class foo {};
>
> class bar {
> public:
> foo foo();
> };
>
> It's clear nonsense because class bar should simply have the operator foo
> instead of oddly named function.


Is it? Again, suppose you have a class color, and a class pen. Then you say
the class pen should have an operator color instead of a member function
color ? Just because the names match ? If this is the case, I would rather
not have you coding in my company.

Best regards,
Ares Lagae
 
Reply With Quote
 
Richard Herring
Guest
Posts: n/a
 
      09-23-2004
In message <u4A4d.3048$(E-Mail Removed)09.us.to.verio .net>,
Victor Bazarov <(E-Mail Removed)> writes
>Ares Lagae wrote:
>> When adopting the coding style of the standard C++ library, you often run
>> into naming problems because class names are lower case, and member
>> functions do not have get/set prefixes.

>
>No, I don't run into issues like that, why do you think I do?
>
>> [...]
>> Is there somewhere more information available about issues like this ?

>
>Well, the standard library naming convention never struck me an confusing,
>and whatever convention we're following here at work is a convention and
>is supposed to make sense to everybody to be adopted. So, no issues so
>far.
>
>As to your example
>
>class foo {};
>
>class bar {
>public:
> foo foo();
>};
>
>It's clear nonsense because class bar should simply have the operator foo
>instead of oddly named function.
>

Hardly nonsense: you may not want an implicit conversion to foo. Giving
the function the name of the thing it returns is in some circumstances a
sensible solution. The OP can probably solve his problem by judicious
use of ::foo .

--
Richard Herring
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      09-23-2004
Ares Lagae wrote:
> Victor Bazarov wrote:
>
>
>>Ares Lagae wrote:
>>
>>>When adopting the coding style of the standard C++ library, you often run
>>>into naming problems because class names are lower case, and member
>>>functions do not have get/set prefixes.

>>
>>No, I don't run into issues like that, why do you think I do?

>
>
> Suppose you have a class color, and a class pen. Adopting the coding style
> of the standard C++ library, pen would have a member function "color
> pen::color()", and you would have a problem. You can solve this problem
> like the C++ standard library does: rename the member function, and
> introduce a less logical name. I hope you recognize this situation is
> identical to the the "locale ios_base::getloc()" example.


To get the color you use 'getcolor', to set it you use 'setcolor'.
You could have

operator color& ();

and

operator color() const;

if you want to use your 'pen' where 'color' is expected.

> When using aggregation, sometimes the role name can be used. For example,
> when you have a class person and a class account, you would introduce a
> member function "person account::holder()" instead of "person
> account:erson". However, there are many cases in which a role name is not
> obvious (like in the pen/color example). Then you run into the problem I
> describe.


No, once again, _I_ don't run into problems like that. It seems that
you're creating your own problems.

> I explicitely mentioned I did not want to discuss coding style, or wether
> the names in the standard C++ library are logical or not...


What did you want to discuss? Some kind of solution to purposely have
the names of members the same as names of accessor functions? Or names
of members the same as type-ids? Perhaps if you just explained what is
it you're trying to ultimately achieve...

>
>
>>As to your example
>>
>>class foo {};
>>
>>class bar {
>>public:
>> foo foo();
>>};
>>
>>It's clear nonsense because class bar should simply have the operator foo
>>instead of oddly named function.

>
>
> Is it? Again, suppose you have a class color, and a class pen. Then you say
> the class pen should have an operator color instead of a member function
> color ?


Yes.

> Just because the names match ?


No. Because that _makes_sense_.

> If this is the case, I would rather
> not have you coding in my company.


I am not coding in your company, nor do I want to (judging by the problems
you seem to have).

V
 
Reply With Quote
 
Richard Herring
Guest
Posts: n/a
 
      09-23-2004
In message <55C4d.3052$(E-Mail Removed)09.us.to.verio .net>,
Victor Bazarov <(E-Mail Removed)> writes
>Ares Lagae wrote:
>> Victor Bazarov wrote:
>>
>>>Ares Lagae wrote:


[...]
>>>

>> Suppose you have a class color, and a class pen. Adopting the
>>coding style
>> of the standard C++ library, pen would have a member function "color
>> pen::color()", and you would have a problem. You can solve this problem
>> like the C++ standard library does: rename the member function, and
>> introduce a less logical name. I hope you recognize this situation is
>> identical to the the "locale ios_base::getloc()" example.

>
>To get the color you use 'getcolor', to set it you use 'setcolor'.
>You could have
>
> operator color& ();
>
>and
>
> operator color() const;
>
>if you want to use your 'pen' where 'color' is expected.


I don't see anything in what AL has posted that suggests that he wants
to do this. He just wants the member function pen::color() to return
something of type ::color. It isn't difficult; he just needs to use a
scope resolution operator to distinguish them.

[...]
>>
>>>As to your example
>>>
>>>class foo {};
>>>
>>>class bar {
>>>public:
>>> foo foo();
>>>};
>>>
>>>It's clear nonsense because class bar should simply have the operator foo
>>>instead of oddly named function.

>> Is it? Again, suppose you have a class color, and a class pen. Then
>>you say
>> the class pen should have an operator color instead of a member function
>> color ?

>
>Yes.
>
>> Just because the names match ?

>
>No. Because that _makes_sense_.


You _appear_ to be advocating that any class A which happens to contain
a B should have an implicit conversion to B. I hope you don't mean it;
most people would say that was a highly evil thing to do.

--
Richard Herring
 
Reply With Quote
 
Ares Lagae
Guest
Posts: n/a
 
      09-24-2004

> It isn't difficult; he just needs to use a
> scope resolution operator to distinguish them.


Yes, I am aware that the scope resolution operator solves the problem. I was
wondering how to extend the "class foo foo()" syntax to the class template
case, and why changing the public and private part helps in the class
template case, but not when foo isn't a class template ...


>>
>>No. Because that _makes_sense_.

>
> You _appear_ to be advocating that any class A which happens to contain
> a B should have an implicit conversion to B. I hope you don't mean it;
> most people would say that was a highly evil thing to do.


I had the same impression.

Ares Lagae

 
Reply With Quote
 
Richard Herring
Guest
Posts: n/a
 
      09-24-2004
In message <(E-Mail Removed)>, Ares Lagae
<(E-Mail Removed)> writes
>
>> It isn't difficult; he just needs to use a
>> scope resolution operator to distinguish them.

>
>Yes, I am aware that the scope resolution operator solves the problem. I was
>wondering how to extend the "class foo foo()" syntax to the class template
>case,


I wasn't thinking of class foo foo() but ::foo foo(). Or N::foo foo(),
if everything is within namespace N.

>and why changing the public and private part helps in the class
>template case, but not when foo isn't a class template ...


I suspect it's not public vs. private but order of declaration that's
the problem. Once you have declared member function foo, in the context
of class bar the unqualified name now means the member function, not the
type, so you have to qualify it to get the type.

--
Richard Herring
 
Reply With Quote
 
Ares Lagae
Guest
Posts: n/a
 
      09-24-2004
Richard Herring wrote:


>>and why changing the public and private part helps in the class
>>template case, but not when foo isn't a class template ...

>
> I suspect it's not public vs. private but order of declaration that's
> the problem. Once you have declared member function foo, in the context
> of class bar the unqualified name now means the member function, not the
> type, so you have to qualify it to get the type.


Indeed. But then I wonder why changing the order of declaration helps in the
case of the template classes, but does not help in the case of the
non-template classes ... Anyway, it does not really matter, but I'm just
curious

Ares Lagae

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
identical function names in and out a class can't compile ciccio C++ 3 01-11-2008 01:00 PM
FastRI, handling identical class names and methods Daniel Berger Ruby 3 12-04-2006 06:58 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
parse error in gcc but success in vc.net, call a non_template class's template member function from a template class's member function! ken C++ 2 06-28-2005 06:57 AM
Pointer-to-member-function pointing to a member function of an inherited class akiriwas@gmail.com C++ 12 02-11-2005 05:15 PM



Advertisments