🔒 Locked
c/c++ structs and pointers question
Advertisement
Original Post
if i have the following struct: struct x{ int a; int b; }; and in the main() we have: struct x* k; i know i can access a member of k via (*k).a or k->a but i always wondered if both works the same way, or one is different from the other? and if why, and which one is more optimal? thank you
-> is what is known as syntactic sugar. Something that makes your code look nice but adds no value or power to the language.
Also in C++ struct behaves exactly like a class, the only difference is the members of a struct are public whereas class members default to private.Well thats what I know anyways [smile]
struct Foo
{
int x;
int y;
};
x and y are public.
class Bar
{
int x;
int y;
};
x and y are private.
Am I correct ?
struct Foo
{
int x;
int y;
};
x and y are public.
class Bar
{
int x;
int y;
};
x and y are private.
Am I correct ?
Black Knight:
Quote:
The Most Holy C++ Standard, psalm 9.4 (Blessed Be Its Subsections)
A structure is a class defined with the class-key struct; its members and base classes (clause class.derived) are public by default (clause class.access). A union is a class defined with the class-key union; its members are public by default and it holds only one data member at a time (class.union).
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement