A
Ares Lagae
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
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