I cant understand the meaning of that code

class Singleton {
public:
static Singleton* instance();
protected:
Singleton();
private:
static Singleton* _instance;
};


First of all it is a very bad code because 1) the function member instance();
and the data member _instance have almost identical names; 2) names started with underscore are reserved by the implementation.

As for the symantic of the code it means that 1) only one object of the class will be created; 2) clients can access the object only by using static function instance().

I can guess that this function defined as the following

1
2
3
4
5
6
static Singleton* Singleton::instance()
{
   static Singleton obj;

   return ( &obj );
}

Last edited on
i can`t understand what 'static Singleton* instance();' means
closed account (zb0S216C)
vlad from moscow wrote:
"1) the function member instance(); and the data member _instance have almost identical names;"

This is common practice and is perfectly valid, as well as readable. Not a bad point at all.

A "Singleton" is a class that allows only 1 instance of the class to be created. The only instance is within the class implementation. Singleton::instance() returns a pointer to the instance within the class. The reason why the internal instance is static is because static members are shared between all instances of the class.

Singleton classes are typically used to prevent multiple "handles" to a specific object (all handles must reference the internal instance). For example, a file can have multiple readers at any given time. By opening the file with a singleton class, only 1 read/write operation can be performed at any given time.

Wazzak
Last edited on
@Framework This is common practice and is perfectly valid, as well as readable. Not a bad point at all.


It is common practice only among those who is a very bad programmer. Names starting with underscore are reserved by the implemenntation and shall not be used in code. Moreover it is very simple to make a typo and insert the undescore or remove it before the name where it is needed or not needed.
closed account (o3hC5Di1)
Names starting with underscore are reserved by the implemenntation and shall not be used in code


Could you please explain that a little bit more for me?
Just interested :)

All the best,
NwN
closed account (zb0S216C)
@NwN: Look here, pal: http://stackoverflow.com/a/228797

vlad from moscow wrote:
"It is common practice only among those who is a very bad programmer."

Sorry, I haven't been reading my copy of "Vlad's C++ Coding Standard". My bad.

It's true that an identifier proceeded by an underscore is reserved, but an underscore after the identifier is not.

Wazzak
Last edited on
The problem is that when one idiot wrote in its own book a bad code all novicies began to repeat this bad code in their practice because have no brain. And it is required much time before they will understand that this code is bad.

Think about the following example

int variable();
int variable_;

// some other code

f( variable );

Can you say what argument is used in the function?! Is it a function name or is it a typo and instead of variable_ there was written variable?

Such code is very difficult to read!
Last edited on
closed account (zb0S216C)
vlad from moscow wrote:
"The problem is that when one idiot wrote in its own book a bad code all novicies began to repeat this bad code in their practice because have no brain. And it is required much time before they will understand that this code is bad. "

I have no idea what you've just said.

As for the code, the issue of ambiguity falls down to the programmer's ability to type both variable_ and variable() correctly - If you don't, it's your fault. This convention is used a lot, which tells me it's good enough to use. Not once have I ever confused a function identifier with a data member identifier.

All in all, nothing is ever good practice in the "Vlad's C++ Coding Standard" book.

Wazzak
Last edited on
closed account (o3hC5Di1)
@Framework - Thanks! Could you please clarify the use of the word "implementation" in that article?

I'm understanding it as "using an underscore-prefix within a scope, shows that that variable is, for instance, a class property, rather than a global variable", thus implementation meaning here "the scope where you declare / use the variable?

Thanks again,

All the best,
NwN
closed account (zb0S216C)
Given the said quote, I would say "implementation" refers to the class definition.

Wazzak
closed account (o3hC5Di1)
Right - thanks!

NwN
@@ramework
As for the code, the issue of ambiguity falls down to the programmer's ability to type both variable_ and variable() correctly - If you don't, it's your fault.


Ha, ha! As I have understood you never make a typo. But except you all other people can make a typo. And it is very simple to make a typo if two names differ only in one end symbol that is underscore. Moreover it is possible that there are two overloaded functions in a project one of which takes as argument a scalar value variable_ and other takes as argument a function name variable. And you even will not know what is the problem with your code then it will behave differently.

O'k it is a very *good* style of programming to make live of others harder! Good lluck!:)

closed account (zb0S216C)
vlad from moscow wrote:
"As I have understood you never make a typo. But except you all other people can make a typo."

Sarcasm doesn't work with me.

vlad from moscow wrote:
"And it is very simple to make a typo if two names differ only in one end symbol that is underscore."

The compiler error would be enough for me to determine what I was meant to write. However, unlike some, I take enough care with my spelling in code to never run into that problem.

vlad from moscow wrote:
"Moreover it is possible that there are two overloaded functions in a project one of which takes as argument a scalar value variable_ and other takes as argument a function name variable. And you even will not know what is the problem with your code then it will behave differently."

That's just plain stupidity. If you write your code like a headless chicken, then you're bound to write confusing code.

vlad from moscow wrote:
"O'k it is a very *good* style of programming to make live of others harder! Good lluck!:)"

My impression of you:
"Guy: Is this code good?" "You: No! It is very bad and means your a bad programmer!"

Wazzak
Last edited on
I am sorry but after your statements that

@ramework
As for the code, the issue of ambiguity falls down to the programmer's ability to type both variable_ and variable() correctly - If you don't, it's your fault.


or

@ramework
That's just plain stupidity. If you write your code like a headless chicken, then you're bound to write confusing code.


it is not seriously to discuss the question with you further. I only can conclude that you did not deal with big projects and you are a big problem for every team of programmers because you will write code that provokes errors.
Last edited on
closed account (zb0S216C)
vlad from moscow wrote:
"I only can conclude that you did not deal with big projects and you are a big problem for every team of programmers because you will write code that provokes errors."

Who are you to question my abilities without actually knowing my abilities? Does it make you feel big when you belittle someone? I don't see you as a superior, nor do I see you as a role model for beginners; so for you to make such a remark is absurd. To be perfectly fair, I think your an absolute w****r who has no right to make such remarks. You really piss me off, do you know that?

Wazzak
Topic archived. No new replies allowed.