Classes in C++

Hello,

I have a question regarding classes in C++. As far as I've understood the built-in types like char, int, double are not classes. Whereas std::vector, std::string, std::map (for example) are classes(?), just as the classes we define ourselves. When working with classes in C++, we use the "."-notation to access members of the class. For example:
1
2
3
4
5
6
7
Class MyClass {
private:
int a;
public:
MyClass(int number) : a{number} {}
int getMyClassValue();
};

and
1
2
MyClass obj;
obj.getMyClassValue();


whereas for a string we can also use the "."-notation to use the member functions of the string class (like size(), length() etc..). So my question is: if you can use the "." on something in C++ that is not a self-defined class(like my MyClass example), is it then a class?
Last edited on
std::string is a class, even though you didn't write it yourself.
Yeah, thx. I kinda wrote that at the beginning of my post, altho I wasn't 100% sure. My question remains though: Is the "." reserved for accessing members of a class ?
Last edited on
Yes, the dot operator is only used to access members of a class. It's not (yet) possible to overload the dot operator to do other things.
if you can use the "." on something in C++ that is not a self-defined class(like my MyClass example), is it then a class?


It is, but having the ability to have "." used on it is not what makes something a class.
There's some imprecise language being thrown around here, and that can lead to confusion. To be absolutely clear and precise:

The "dot" operator is used to access members of an object, not a class.

The :: operator is used to access members of a class.

Be clear on what the difference is. A class is a type. An object is an instance of that type.
Last edited on
Topic archived. No new replies allowed.