473,395 Members | 1,677 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,395 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 1683
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.