What's wrong withthe code?

D

Dart

This code is okay:

class CSingle
{
public:
static CSingle membs;
int MEM_NUM;
CSingle(int n)
{
}
};

CSingle CSingle::membs(5);

However if it's changed as:

class CSingle
{
public:
static CSingle membs;
int MEM_NUM;
CSingle()
{
}
};

CSingle CSingle::membs();

Compiler spits errors:

error C2063: 'membs' : not a function
error C2040: 'membs' : 'class CSingle (void)' differs in levels of
indirection from 'class

Does anyone know the reason for the problem?
Is this a good way to implement Singleton? Thanks!
 
J

John Carson

Dart said:
This code is okay:

class CSingle
{
public:
static CSingle membs;
int MEM_NUM;
CSingle(int n)
{
}
};

CSingle CSingle::membs(5);

However if it's changed as:

class CSingle
{
public:
static CSingle membs;
int MEM_NUM;
CSingle()
{
}
};

CSingle CSingle::membs();

Compiler spits errors:

Drop the brackets, i.e., make it

CSingle CSingle::membs;
 
D

David Harmon

On Sun, 18 Apr 2004 04:56:08 GMT in comp.lang.c++, "Dart"
CSingle CSingle::membs();

Compiler spits errors:

error C2063: 'membs' : not a function

Indeed.
CSingle CSingle::membs; // static member variable previously declared
CSingle CSingle::membs(); // undeclared member function with no args.
 
D

Dart

John Carson said:
Drop the brackets, i.e., make it

CSingle CSingle::membs;
It was what I did to rid the compile error though I did not understand.

Why did "CSingle::membs(5)" work with constructor "CSingle(int n)", but
"CSingle::membs()" did not with "CSingle()"?

How does "static CSingle membs;" really do the magic here? Thanks!
 
D

David Harmon

On Sun, 18 Apr 2004 16:30:14 GMT in comp.lang.c++, "Dart"
It was what I did to rid the compile error though I did not understand.

Why did "CSingle::membs(5)" work with constructor "CSingle(int n)", but
"CSingle::membs()" did not with "CSingle()"?

How does "static CSingle membs;" really do the magic here? Thanks!

How do you write the declaration of a simple function, one with no
arguments? What do you think is the difference between that declaration
and the one where the compiler tells you that there is something wrong
with your declaration of function membs() ?
 
K

Kevin Goodsell

Dart said:
This code is okay:

class CSingle
{
public:
static CSingle membs;

Can you really have a static member that is the same type as the class
it is a member of? What would that even mean? Would you have infinitely
nested 'membs' that are really all the same object?

-Kevin
 
D

David Harmon

This code is okay:

class CSingle
{
public:
static CSingle membs;

Can you really have a static member that is the same type as the class
it is a member of? What would that even mean? Would you have infinitely
nested 'membs' that are really all the same object?[/QUOTE]

No problem. The static var is not "nested", it just sits there.
Csingle has both kinds of instances, static and automatic.
One of them happens to be static Csingle:: qualified.
 
R

Rob Williscroft

Kevin Goodsell wrote in @newsread1.news.pas.earthlink.net in comp.lang.c++:
Can you really have a static member that is the same type as the class
it is a member of? What would that even mean? Would you have infinitely
nested 'membs' that are really all the same object?

static members are members of the class (of which there is only one).

So the static instance's class has a member which is the instance
itself, recursion would be if the instance *contained* itself, which
it doesn't.

Similar perhaps to an object that contains a /pointer/ to itself.

Rob.
 
K

Kevin Goodsell

Rob said:
Kevin Goodsell wrote in @newsread1.news.pas.earthlink.net in comp.lang.c++:



static members are members of the class (of which there is only one).

So the static instance's class has a member which is the instance
itself, recursion would be if the instance *contained* itself, which
it doesn't.

Similar perhaps to an object that contains a /pointer/ to itself.

Rob.

So the following is legal, if I understand correctly:

#include <iostream>

struct Test
{
static Test t;
};

Test Test::t;

int main()
{
Test a;

if (&(a.t.t.t.t.t.t.t.t.t.t.t.t.t.t.t.t.t) == &(a.t))
{
std::cout << "Hmmmm..." << std::endl;
}

return 0;
}

g++ (-W -Wall -ansi -pedantic) accepts this and prints "Hmmmm...". It
shows what I meant by "infinitely nested [objects] that are really all
the same object". It makes sense (sort of), but seems strange to me.

-Kevin
 
R

Rob Williscroft

Kevin Goodsell wrote in @newsread1.news.pas.earthlink.net in comp.lang.c++:
So the following is legal, if I understand correctly:

AFAICT Yes.
#include <iostream>

struct Test
{
static Test t;
};

Test Test::t;

int main()
{
Test a;

if (&(a.t.t.t.t.t.t.t.t.t.t.t.t.t.t.t.t.t) == &(a.t))
{
std::cout << "Hmmmm..." << std::endl;
}

return 0;
}

g++ (-W -Wall -ansi -pedantic) accepts this and prints "Hmmmm...". It
shows what I meant by "infinitely nested [objects] that are really all
the same object". It makes sense (sort of), but seems strange to me.

#include <iostream>
#include <ostream>

struct X
{
X *x;
X() : x( this ) {}
};

int main()
{
X x[1];
if (x->x->x->x->x->x->x->x->x->x == x)
{
std::cout << "Yup" << std::endl;
}
}

Of course you code gets compiled as if you wrote:

if( &test::t == &test::t )

So you could get a "Condition is always true" warning (*).
The best I got was from VC 7.1 which tells me that
local 'a' is unreferenced.

*) My ideal compiler would give this warning, as long as
the condition doesn't depend an a template paramiter,
as in this case.


Rob.
 
J

John Carson

Kevin Goodsell said:
Can you really have a static member that is the same type as the class
it is a member of? What would that even mean? Would you have
infinitely nested 'membs' that are really all the same object?

-Kevin


Stroustrup (TC++PL, pp. 228-9) discusses a Date class that has a static
member that is a Date object. You might like to look at it.
 
K

Karl Heinz Buchegger

Dart said:
Why did "CSingle::membs(5)" work with constructor "CSingle(int n)", but
"CSingle::membs()" did not with "CSingle()"?

Rule: If something can be a function declaration (aka a prototype) then
it is a function declaration.

int main()
{
int j;
CSingle foo2( j );

This cannot be a function declaration, since there would need to be a
data type in the argument list, and j isn't a data type.

CSingle foo3( int j );

This looks like a function declaration, thus it is one.

CSingle foo();

foo, looks like a function declaration, thus it is one. foo is a function
which takes no arguments and returns a CSingle object.

How does "static CSingle membs;" really do the magic here? Thanks!

static has nothing to do with it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

what's wrong in my code?!? 5
Single instance 1
What's Wrong with this code 5
My Status, Ciphertext 2
Problem with codewars. 5
read chars from a string 4
what's wrong of my code 2
Behavior of the local class 3

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,156
Latest member
KetoBurnSupplement
Top