answersLogoWhite

0

Can you return a reference to public data member?

Updated: 8/18/2019
User Avatar

Wiki User

10y ago

Best Answer

Yes, but since it is public there is no need to; any code outwith the class can access a public data member directly. If it were a private data member, then returning by reference would defeat the point of making it a private data member in the first place; you might as well make it public. Private data members should always be returned by value, never by reference and never by pointer, and data members should only be declared public if they are not critical to the internal operation of the class.

User Avatar

Wiki User

10y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Can you return a reference to public data member?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Can member data be public?

Of course.


Only public member functions can access public member data TrueFalse?

False. Public member data is accessible to all functions, whether they be public, protected or private members of the same class, or they are outside of the class completely.


Is there anything similar between C and C plus plus with reference to data hiding?

No. Data hiding is a feature of object oriented programming. C does not support OOP, and therefore has no private member access. All members are public in C.


How would you access data members of a class in cases inside member function of another class?

Either make the data members public, or make the member function a friend of the class containing the data member.


What is IRS reference number 1102?

1102 No data, taxpayer filed electronic return more than 3 weeks ago See IRM 21.4.1.3.1.1, Return Not Found


Where can one access their credit data?

To access their credit data, people should send a letter to a credit reference agency. They collect public and credit data to produce credit reports and credit scores.


Can you access in drive class from base class private data?

Base class should no knowledge about derived classes. The "private" modifier on a data member means private to the class which defined it. Base class cannot directly reference/access the private data member of the derived class, and the derived classes cannot access the private data member defined in the base class. Either way the accessing the private data member should be done via properties or getters


Difference friend and public access modifier?

In C++, a friend function or friend class can grant access to its private data members to other classes. The public member allows any class to access that data.


What are data members and members function?

The correct terminology is member variables and member methods. They are both members of a class. The member variables are used to store an object's data, while the member methods are used to operate upon that data.


What is the difference between static data member and ordinary data member?

Static data is data that does not change from program load to program exit. Static data member do not apply for c. In c++, a static data member is one that is common for all instances of that class.


What is cell reference that contain numeric data?

a. Write down the cell reference of a cell the contains numeric data


What is classes in programing?

There are no classes in C; it is not an object-oriented programming language. C++ has classes. A class is a data type from which objects can instantiated in much the same way that an integer variable can be instantiated from an int data type in both C and C++. However, an int is a primitive data type; it has no member methods associated with it. The built-in operators are designed to operate upon primitive data types but those operators are not integral to the type. A class is more like a struct in C; an aggregate of data values. A class can contain both static data (data that is common to the class) and non-static data (data that relates to an instance of the class). However, as well as storing data, a class can also define member functions that operate upon that data, but that are scoped to the class (static member functions) or to an instance of the class (instance member functions). Unlike C where a struct's data members are always public, a C++ class can define separate public, protected and private data members (and functions), where private is the default access. A C++ struct is also a class, but one where the members are public by default. As such, a C++ struct can be used to create trivial "plain old data" classes that are compatible with C code as well as to create highly complex data types. Objects are self-contained entities where the member methods (functions and operators) have private access to the class representation. Non-member functions cannot gain access to this representation other than through public member functions or by being declared a friend of the class. The protected representation is the same as the private representation but is also accessible to derivatives of the class. Derivatives automatically inherit the public and protected members of their base classes, but not the private members. This makes it possible to derive more specialised classes from existing classes without have to duplicate the base class code.