Rick
Rick

Reputation: 7506

what's difference between defining a class type member as public and private?

class Screen{
public:
      typedef std::string::size_type pos;
private:
      pos cursor = 0;
      pos height = 0, width = 0;
      std::string contents;
};

I've tried constructing an instance of Screen. Neither can access pos. By the way, I know access control. I just wanna know the meaning of access control for a type member.

Upvotes: 0

Views: 127

Answers (5)

Evan
Evan

Reputation: 528

Public means that anyone (other classes) can touch those variables. Private means that only you (your methods) can touch those variables.

There is a caveate, which is introducing "friends", which give a function or class that isn't you, access to all of the variables.

Member types and member variables work in the same way. A private type won't be available beyond the methods and friends of a class, just as a private method or variable. Conversely, a public type, method, or variable, will be accessible to anyone. Unlike with (non-static) methods and variables, types have to be static.

For example, this will fail to compile since pos is private.

#include <string>
class Test {
  private:
    using pos = std::string::size_type;
  public:

};

int main(int argc, char *argv[])
{
  Test::pos a = 5;

  return 0;
}

To access the type pos from outside of the screen class, first move the typedef to public accessibility. Then it will be available with screen::pos, just as with a static method or variable.

The following should compile just fine though (maybe a warning about a being unused, but that's a different issue.)

#include <string>
class Test {
  public:
    using pos = std::string::size_type;    
};

int main(int argc, char *argv[])
{
  Test::pos a = 5;

  return 0;
}

You can go a little crazy with this and even define an entire class internally as being either public or private too.

Upvotes: 3

Rick
Rick

Reputation: 7506

Type member is different from data member. It's more like a static stuff which I can't access it through an instance. So defining it as public or private deciding whether I can access it through Screen::pos. That's it.

Upvotes: 0

Joseph D.
Joseph D.

Reputation: 12174

From class.access#4

Because access control applies to names, if access control is applied to a typedef name, only the accessibility of the typedef name itself is considered. The accessibility of the entity referred to by the typedef is not considered.

class A {
  class B { };
public:
  typedef B BB;
};

void f() {
  A::BB x;          // OK, typedef name A​::​BB is public
  A::B y;           // access error, A​::​B is private
}

Thus, doesn't mean your type is public would make the member variable have public access. typedef is for naming types not for specifying a member's access.

Upvotes: 0

Eduard Rostomyan
Eduard Rostomyan

Reputation: 6546

Because although your typedef is public, the actual member is declared private. See C++ class access specifiers for more info.

Upvotes: 0

Muhammad Usman
Muhammad Usman

Reputation: 1258

Source:https://www.tutorialspoint.com/cplusplus/cpp_class_access_modifiers.htm

The public Members

A public member is accessible from anywhere outside the class but within a program. You can set and get the value of public variables without any member function as shown in the following example:

Code:

#include <iostream>

using namespace std;

class Line {
   public:
      double length;
      void setLength( double len );
      double getLength( void );
};

// Member functions definitions
double Line::getLength(void) {
   return length ;
}

void Line::setLength( double len) {
   length = len;
}

// Main function for the program
int main() {
   Line line;

   // set line length
   line.setLength(6.0); 
   cout << "Length of line : " << line.getLength() <<endl;

   // set line length without member function
   line.length = 10.0; // OK: because length is public
   cout << "Length of line : " << line.length <<endl;

   return 0;
}

Output of above code:

Length of line : 6
Length of line : 10

The Private Members:

A private member variable or function cannot be accessed, or even viewed from outside the class. Only the class and friend functions can access private members.

By default all the members of a class would be private, for example in the following class width is a private member, which means until you label a member, it will be assumed a private member:

class Box {
   double width;

   public:
      double length;
      void setWidth( double wid );
      double getWidth( void );
};

Practically, we define data in private section and related functions in public section so that they can be called from outside of the class as shown in the following program.

Code

#include <iostream>

using namespace std;

class Box {
   public:
      double length;
      void setWidth( double wid );
      double getWidth( void );

   private:
      double width;
};

// Member functions definitions
double Box::getWidth(void) {
   return width ;
}

void Box::setWidth( double wid ) {
   width = wid;
}

// Main function for the program
int main() {
   Box box;

   // set box length without member function
   box.length = 10.0; // OK: because length is public
   cout << "Length of box : " << box.length <<endl;

   // set box width without member function
   // box.width = 10.0; // Error: because width is private
   box.setWidth(10.0);  // Use member function to set it.
   cout << "Width of box : " << box.getWidth() <<endl;

   return 0;
}

Output of Above code:

Length of box : 10
Width of box : 10

Upvotes: 0

Related Questions