Lingxiao Zhang
Lingxiao Zhang

Reputation: 45

class and static variables

#include <iostream>
using namespace std;
class A {
public:
    static int a;
    A() {a=1;a++;}

};
int A::a =1;

int main(void){
 A a;

 cout<<A.a<<endl;
 cout<<a<<endl;
 cout<<a.a<<endl;


 return 0;
 }

I'm a beginner to C++ and studying class now. I'm just wondering why can I only print out the last one(a.a)? What are the problems for A.a and a? Thanks in advance.

Upvotes: 2

Views: 72

Answers (5)

    cout<<A.a ;//invalid in c++ but valid in java and 
               //some other languages  

is not a valid syntax and you have to use A::a (pronounced as scope resolution (in the scope of class A))

   cout<<A::a ;//valid in c++

 cout<<a; //Here a is an instance that has some 
          //characteristics 

So you have to use stream insertion overloading in c++ to overcome that.

Finally,

        cout<<a.a; //well and good as it an attribute of class A.

Moreover ,it is a static attribute(shared by other objects).Changes made to that reflects to other objects characteristics.Refer this

One final suggestion is to refer good oops concept book.

Upvotes: 1

Roshan Br
Roshan Br

Reputation: 360

In your posted code:

 cout<<A.a<<endl;

The above line is syntactically incorrect, you should write it as A::a

cout<<a<<endl;

The above line is trying to de-reference an object (Instance of Class A) and the compiler doesn't know how to print.

 cout<<a.a<<endl;

This prints because here you are correctly accessing a static variable via an instance of class A.

Upvotes: 0

Slava
Slava

Reputation: 44268

What are the problems for A.a and a?

A is a type, but operator . is only applicable to objects (or references to them). If you want to access static variable use different syntax:

std::cout << A::a;

As for why std::cout << a does not work, std::ostream does not know how to output instance of class A, you can create your own operator for that:

std::ostream &operator<<(std::ostream &out, const A &a )
{
   return out << a.a;
}

then this code in main will work:

std::cout << a;

Upvotes: 1

Quimby
Quimby

Reputation: 19223

A.a is not valid, because left operand of the dot operator must be object, not a type. A::a would be valid syntax for that.

std::cout<<a; actually translates to a call of the function std::ostream& operator<<(std::ostream& out,[can be const] A& a) and there isn't one, so the compiler doesn't know how to print object of type A. You can implement that function e.g.:

std::ostream& operator<<(std::ostream& out,const A& a)
{
    out<<"Hello, I'm object A\n";
    return out;
}

Upvotes: 0

mister why
mister why

Reputation: 1987

You have an instance of A that you named a.

The first line is syntactically wrong because you want to access a static field through the type. In C++, it's written A::a.

On the second line, you want to print the instance of A, not the content of its field. Since you did not define a way to print an instance of A, the build fails. (You will see streams later I guess).

And eventually, the last line is fine because you can access a static field by an instance as well.

Upvotes: 0

Related Questions