PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, July 8, 2022

[FIXED] How to understand Using :: (Scope resolution operator) to access a in-class class (nested class) or typedef

 July 08, 2022     c++, class, scope, typedef     No comments   

Issue

I'm trying to understand Scope resolution operator ::
I know I can only access static class member via Scope resolution operator.
But I can use it to access typedef or a nested class thing like this:

class test{
    public:
    class testinner{
        public:
        int _val;
        testinner(){}
        testinner(int x):_val(x){}
    };
    test(){}
    typedef int testdef;


    int s;
};

int main()
{
    test::testinner tt1 = test::testinner(5); //OK LINE(1)
    test::testinner tt2; //OK LINE(2)
    test::testdef tt3 = 5; //OK LINE(3)
    test::s = 5; //non static member ERROR LINE(4)

    return 0;
}

I can instantiate an in-class type object via :: such as line 1 and line 2
I can use typedef to instantiate an object such as line 3
I can't access non-static members via :: such as line 4

Is that mean an in-class class and typedef is a static member in a class? I know namespace is quite equal to class name but I'm still really getting confused about it.

By the way, for the typedef part, can I simply think tt3 is int type rather than test::testdef type?


Solution

I can't access non-static members via :: such as line 4

False. The problem is that you cannot access non-static members without an object. If you have an object, you can use a qualified name (with ::).

int main()
{
    test t;
    t.test::s = 5;
    //^^^^^^
}

Is that mean an in-class class and typedef is a static member in a class?

It depends what you mean by "static member". They do not require an object of the class, but at the same time they do not require the static keyword. (My understanding is that in the official terminology, nested types and typedefs are not considered "members", so in that respect they are not static members. However, I think that might be side-stepping the intended question.)

By the way, for the typedef part, can I simply think tt3 is int type rather than test::testdef type?

Up to you. A typedef creates an alias, so int and test::testdef are two names for the same thing. If you prefer thinking in terms of int, do that. If you prefer thinking in terms of test::testdef, do that. The type doesn't mind which name you use.

(The same would hold if the typedef was outside a class definition. You are making the situation less clear for yourself by thinking that the class makes a difference in this case. A typedef defines an alias for a type.)



Answered By - JaMiT
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing