473,976 Members | 2,878 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

member function names identical to class names

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::s tr()".
It would be more logical to name it "string stringstream::s tring()".
- The class ios_base has a member function "locale ios_base::getlo c()". It
would be more logical to name it "locale ios_base::local e()". 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
Jul 22 '05 #1
8 2112
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
Jul 22 '05 #2
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::getlo c()" 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::person ". 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
Jul 22 '05 #3
In message <u4************ ****@newsread1. dllstx09.us.to. verio.net>,
Victor Bazarov <v.********@com Acast.net> 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
Jul 22 '05 #4
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::getlo c()" 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::person ". 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
Jul 22 '05 #5
In message <55************ ****@newsread1. dllstx09.us.to. verio.net>,
Victor Bazarov <v.********@com Acast.net> 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::getlo c()" 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
Jul 22 '05 #6
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

Jul 22 '05 #7
In message <10************ ***@seven.kulne t.kuleuven.ac.b e>, Ares Lagae
<ar********@cs. kuleuven.ac.be> 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
Jul 22 '05 #8
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

Jul 22 '05 #9

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

Similar topics

9
4980
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my webserver runs that part of the script (see attached file, snippet.php), though, it doesn't go through. I don't get an error message or anything...it just returns a "1" (whereas it should return a "0") as far as I can tell. I have read the PHP...
2
3656
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't support. (first i compiled them with g++3.x. ERR means compiler will bark, otherwise it does accept it. Then the Comeau C/C++ 4.3.3 comes)
3
2012
by: Mark Turney | last post by:
Problem: I have a vector full of two different derived class objects (class B and class C) that are derived from the same base class A. I want to loop through vector and invoke a member function in only objects of class B and skip over the objects of class C. To complicate things, I'm using the vector position (index) as an argument in the invoked member function. It is possible to move the position into the object by adding a data...
2
2329
by: Steven T. Hatton | last post by:
I find the surprising. If I derive Rectangle from Point, I can access the members of Point inherited by Rectangle _IF_ they are actually members of a Rectangle. If I have a member of type Point in Rectangle, the compiler tells me Point::x is protected. I would have expected Rectangle to see the protected members of any Point. Compiling the following code give me this error: g++ -o rectangle main.cc main.cc: In member function `size_t...
6
4422
by: Bill Rubin | last post by:
The following code snippet shows that VC++ 7.1 correctly compiles a static member function invocation from an Unrelated class, since this static member function is public. I expected to compile the same invocation from a DistantlyRelated class. What actually happened was that the compiler produced: error C2247: 'A::function' not accessible because 'CloselyRelated' uses 'private' to inherit from 'A' I'm guessing that the above compiler...
114
7950
by: Jonathan Wood | last post by:
I was just wondering what naming convention most of you use for class variables. Underscore, "m_" prefix, camel case, capitalized, etc? Has one style emerged as the most popular? Thanks for any comments. --
6
1873
by: Howard | last post by:
Hi, I have a function in three unrelated but similar classes. The code in the member functions is identical for all three classes. What I want is to make a template which defines the function, implemented as a non-static member of each class. Aside from this function, there is no reason the three classes should inherit from a common base class, which is why I thought a template would be good. But I can't figure out the syntax for a...
13
2551
by: JohnQ | last post by:
The implementation of classes with virtual functions is conceptually easy to understand: they use vtables. Which begs the question about POD structs: how are they associated with their member functions in common implementations? And where is the 'this' ptr tucked away at for POD structs with member functions? John
7
3781
by: PengYu.UT | last post by:
Hi, I want to write a test function to test a class. The class has a private member function that need to be tested. Although I could modify the member function as protected or public, I do not want to modify the class definition. I'm wondering what is the standard way to test private member functions in a library. Thanks, Peng
0
10350
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10167
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11815
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...
0
11406
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7602
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
6413
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
5150
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
4729
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3758
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.