473,320 Members | 1,600 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

What's wrong withthe code?

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!
Jul 22 '05 #1
11 2200
"Dart" <da******@dickto.com> wrote in message
news:IT*********************@bgtnsc04-news.ops.worldnet.att.net
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;
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)
Jul 22 '05 #2
On Sun, 18 Apr 2004 04:56:08 GMT in comp.lang.c++, "Dart"
<da******@dickto.com> wrote,
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.

Jul 22 '05 #3
John Carson <do***********@datafast.net.au> wrote in message
news:40******@usenet.per.paradox.net.au...
"Dart" <da******@dickto.com> wrote in message
news:IT*********************@bgtnsc04-news.ops.worldnet.att.net
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;

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!
Jul 22 '05 #4
On Sun, 18 Apr 2004 16:30:14 GMT in comp.lang.c++, "Dart"
<da******@dickto.com> wrote,
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() ?

Jul 22 '05 #5
Dart wrote:
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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Jul 22 '05 #6
On Sun, 18 Apr 2004 18:47:14 GMT in comp.lang.c++, Kevin Goodsell
<us*********************@neverbox.com> wrote,
Dart wrote:
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?


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.

Jul 22 '05 #7
Kevin Goodsell wrote in news:S2Agc.19892$A_4.3903
@newsread1.news.pas.earthlink.net in comp.lang.c++:
Dart wrote:
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?


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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #8
Rob Williscroft wrote:
Kevin Goodsell wrote in news:S2Agc.19892$A_4.3903
@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.


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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Jul 22 '05 #9
Kevin Goodsell wrote in news:CgDgc.20417$A_4.18012
@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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #10
"Kevin Goodsell" <us*********************@neverbox.com> wrote in
message news:S2******************@newsread1.news.pas.earth link.net
Dart wrote:
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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

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.
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #11
Dart wrote:


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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #12

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

Similar topics

125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
72
by: E. Robert Tisdale | last post by:
What makes a good C/C++ programmer? Would you be surprised if I told you that it has almost nothing to do with your knowledge of C or C++? There isn't much difference in productivity, for...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
51
by: WindAndWaves | last post by:
Can anyone tell me what is wrong with the goto command. I noticed it is one of those NEVER USE. I can understand that it may lead to confusing code, but I often use it like this: is this...
56
by: Cherrish Vaidiyan | last post by:
Frinds, Hope everyone is doing fine.i feel pointers to be the most toughest part in C. i have just completed learning pointers & arrays related portions. I need to attend technical interview on...
46
by: Keith K | last post by:
Having developed with VB since 1992, I am now VERY interested in C#. I've written several applications with C# and I do enjoy the language. What C# Needs: There are a few things that I do...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
98
by: tjb | last post by:
I often see code like this: /// <summary> /// Removes a node. /// </summary> /// <param name="node">The node to remove.</param> public void RemoveNode(Node node) { <...> }
2
by: Author | last post by:
I am trying to write a Windows service or console application that will manage Windows user accounts. In other words, the application will be interacting with the underlying Windows OS, for...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.