472,354 Members | 1,541 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Some questions about static member

Dear all,

I am new with C++ and very confused with some features.
Really appreciate if you can explain to me some of stuffs below.

I define a class:

class A {
static A* instance = 0;
};

then I have error saying that I cannot initialize a static member
inside the class. Why? As a newbie to C++ I don't see why not?

then I move the initialization outside to A.cpp:

A::instance = 0;

then compiler complains and I have to do the following:

A* A::instance = 0;

Here I also don't see why. For example, if I have an interger:

int i;

then I initialize it by:

i = 0;

that is it. Why the instance var above need the A* type declearation.

Basically, as a newbie to C++ I am very confused with huge syntaxs and
semantics. Really want to learn more.

Many thanks in advance!

Tuan-Anh
Jul 23 '05 #1
3 1606
Tran Tuan Anh wrote:
Dear all,

I am new with C++ and very confused with some features.
Really appreciate if you can explain to me some of stuffs below.

I define a class:

class A {
static A* instance = 0;
};

then I have error saying that I cannot initialize a static member
inside the class. Why? As a newbie to C++ I don't see why not?
Because that's how C++ is defined.
then I move the initialization outside to A.cpp:

A::instance = 0;
That wouldn't be an initialization. It would be an assignment, and
assignments are not allowed outside of a function.
The difference between initialization and assignment is that the former
creates a new object and gives it the specified value, whereas an
assignment just gives a new value to an already existing object.
then compiler complains and I have to do the following:

A* A::instance = 0;

Here I also don't see why.
Because in the class definition, you only declare 'instance', i.e. you say
that the class has a member with that name. But it isn't yet definied, i.e.
there is no storage for that object. Therefore, you have to add to one
translation unit a definition of that static member variable.
For example, if I have an interger:

int i;

then I initialize it by:

i = 0;
Again, that's not an initialization, but an assignment. You can tell the
difference by the following rule:

type name = value; <- Initialization
name = value; <- Assignment
that is it. Why the instance var above need the A* type declearation.
It's unclear to me what you mean.
Basically, as a newbie to C++ I am very confused with huge syntaxs and
semantics. Really want to learn more.


I hope I could help a bit.
Jul 23 '05 #2
Tran Tuan Anh schrieb:
Dear all,

I am new with C++ and very confused with some features.
Really appreciate if you can explain to me some of stuffs below.

I define a class:

class A {
static A* instance = 0;
};

then I have error saying that I cannot initialize a static member
inside the class. Why? As a newbie to C++ I don't see why not?
That's only allowed for const integral or enumeration types, instance
above is neither const nor an integral type.
then I move the initialization outside to A.cpp:

A::instance = 0;

then compiler complains and I have to do the following:

A* A::instance = 0;

Here I also don't see why. For example, if I have an interger:

int i;
Compare the two again:
type name
int i;
A* A::instance;

then I initialize it by:

i = 0;
type name initialiser
int i = 0;
A* A::instance = 0;
that is it. Why the instance var above need the A* type declearation.

Just like you need int i = 0; and not just i = 0; Don't let the ::
confuse you.

HTH,
Malte
Jul 23 '05 #3
Thanks all for your prompt replies. I kind of understand now :)

Jul 23 '05 #4

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

Similar topics

5
by: Chutian | last post by:
What's the best way to share data between classes? What's the purpose of define a member function in a class as 'static'? Just for it to access static member variables? Is there any other...
7
by: Tony Johansson | last post by:
Hello Experts! I have the following Array template class see below. I execute these three statements statement 1: Array<int> x(5); statement 2: cin >>x; statement 3: Array<int>::element_type ...
2
by: Servé Lau | last post by:
I have been rewriting a C++ isapi into a C# asp.net application, but because it was my first one and created within a tight schedule I have some questions to make sure if I did it right. 1. Does...
4
by: alisaee | last post by:
plz check what i have made wrong what is requierd her is to creat class queue and class stack and run the push,pop operation . #include<iostream.h> #include<conio.h> #include<stdio.h> class...
2
by: newjazzharmony | last post by:
All, I'd appreciate it if someone could answer these questions for me. This scenario applies to an ASP dot net application that is deployed in a web farm. 1) Does the application live in a...
39
by: Digital Puer | last post by:
I'm not the world's greatest C++ programmer, so I had a hard time with these. Some help would be appreciated. 1. Comment on the declaration of function Bar() below: class Foo { static int...
3
by: mimi | last post by:
1.Instead of using macro, I would like to use static const variables in such situations. class Foo { public: static const int SOMEVALUEWITHFOO = 1; } int main() {
17
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;"...
2
by: .rhavin grobert | last post by:
i have (do try to have?) the following... & = breakpoints in debugger // ---------------------------------------------------------------- // cx.h class CX { public: CX(CX* pcx = NULL);...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
0
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...

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.