Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between these two statements

Tags:

c++

oop

What's the difference between these two statements?

ob.A::ar[0] = 200;
ob.ar[0] = 200;

where ob is an object of class A

class A
{
    public:
        int *ar;
        A()
        {
            ar = new int[100];
        }
};
like image 864
Rushil Paul Avatar asked Oct 14 '12 19:10

Rushil Paul


People also ask

What is the difference between sentences and statements?

A sentence is a group of words that usually have a subject, verb and information about the subject. Remember: A sentence can be a statement, question or command. A statement is a basic fact or opinion. It is one kind of sentence.

What is the difference between an and and statement?

The difference between AND, OR is that AND evaluates both conditions must be true for the overall condition to be true. The OR evaluates one condition must be true for the overall condition to be true. In the OR result, if name is John then condition will be true. If any row has the age 22, then it will be true.

What is the difference between what is this and what is that?

Key Differences Between This and That'This' can be used to refer to something which is just mentioned. Conversely, 'that' refers to something which is previously mentioned or implied. The plural form of the this is 'these', whereas 'those' is the plural form of that.

How can you tell the difference between a question and a statement?

Questions, commands and advice are typically not statements, because they do not express something that is either true or false. But sometimes people use them rhetorically to express statements. We saw an example of a question which by itself is not a statement, but can be used to express a statement.


1 Answers

There is no difference. The explicit namespace qualification of ar is redundant in this case.

It might not be redundant in cases where (multiple, nonvirtual) inheritance redefines the name ar. Sample (contrived):

#include <string>

class A 
{
    public:
        int *ar;
        A() { ar = new int[100]; }
        // unrelated, but prevent leaks: (Rule Of Three)
       ~A() { delete[] ar; }
    private:
        A(A const&);
        A& operator=(A const&);
};

class B : public A
{
    public:
        std::string ar[12];
};


int main()
{
    B ob;
    ob.A::ar[0] = 200;
    ob.ar[0] = "hello world";
}

See it on http://liveworkspace.org/code/d25889333ec378e1382cb5af5ad7c203

like image 76
sehe Avatar answered Sep 30 '22 17:09

sehe