Scope of definition of C + + class

Source: Internet
Author: User

Each class defines its own new scope and unique type. Within the definition body of a class, member names are introduced into the scope of the class. Two different classes have two different class scopes.

For example:

Class First {

int Memi;

Double memd;

};

Class Second {

int Memi;

Double memd;

};

First obj1;

Second Obj2=obj1;//error:obj1and obj2 have different types

The previous example shows that even though two classes have the exact same list of members, they are different types. The members of each class are different from the members of any other class (any other scope). Outside the scope of a class, members can only use the member access operator, respectively, through objects or pointers. Or "," to access. Such as

Class obj;

Class *ptr = &obj;

ptr->member; PTR->MEMFCN ();

Obj.member; OBJ.MEMFCN ();

Alternatively, you can use the scope operator to access the

Double Sales_item::avg_price () const

{

if (units_sold)

return revenue/units_sold;

Else

return 0;

}

In the above example, we use the fully qualified name Sales_item::avg_price to specify that this is the definition of the Avg_price member in the class Sales_item scope. (Once you see the fully qualified name, you know that the definition is in the class scope) because the call to revenue and units_sold in the class scope does not need to be written this->revenue or this->units_sold. also defined in member functions outside the class, the formal parameter list and member function bodies appear after the member names, which are defined in the scope of the class, so that other members can be referenced without qualification.

The return type is defined in front of the member name, as compared to the formal parameter type. If a function is defined outside the class definition, the name used for the return type is outside the scope of the class. If the return type uses a type defined by the class, the fully qualified name must be used. as

Class screen{

Public:

Typedef Std::string::size_type Index;

Index get_cursor () const;

};

Inline Screen::index screen::get_cursor () const

{

return cursor;

}

The function return type is index, which is a type name defined inside the screen class.

The C + + language allows you to declare global variables, and the implementation of class members is outside the class, so you often encounter scope problems. Here Jane lists some of the problems that usually come up.

Parameters in the definition of a class member function

A simple example:

12345678910111213 classT{    public:        typedefdouble Num;        voidsetValue(Num num);    private:        Num value;};voidT::setValue(Num num){    this->value = num;}

Where num is the type defined in the class T, SetValue is the definition of the member function of the class T (outside the class), so SetValue must be within the scope of the class, and of course the formal parameter list is within the scope of the class. So the above usage is legal.

Second, the return value of the member function

12345678910111213 classT{    public:        typedefdoubleNum;        Num getValue();    private:        Num value;};Num T::getValue(){    returnthis->value;}

The type of the return value is in front of the member function body compared to the formal parameter type, so C + + does not consider NUM to be within the scope of the class, and if the above code is compiled, the compiler will error. The correct wording is:

12345678910111213 classT{    public:        typedefdoubleNum;        Num getValue();    private:        Num value;};T::Num T::getValue(){    returnthis->value;}

Scope of definition of C + + class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.