1

I want to know how can i convert a "typedef struct" in objective C to a c++ code preferably make into a class. is it possible ? can i used classes ?

Example :

typedef struct{
    int one;
    int two;
}myStruct;

2 Answers 2

2

There is no fundamental difference between struct and class in C++*. This is a type mystruct with two public data members:

struct mystruct {
    int one;
    int two;
};

This is exactly the same type as

class mystruct
{
  public:
    int one;
    int two;
};

* The difference is that members and base classes are public by default in a struct and private in a class. The two keywords can be used to express the same types.

Sign up to request clarification or add additional context in comments.

6 Comments

There is a difference between the default accessors
@ThePlatypus Right, no fundamental difference. The compiler will not know the difference between class Foo { int i; }; and struct Foo { private: int i; }; for example.
@juanchopanza sorry, missed the word.
Beat me to it with a better answer, have an upvote :). In case it isn't clear to OP or others, members of c++ class types are default PRIVATE, while struct members are default PUBLIC.
@BaylesJ thanks, you made me realise I had messed up the explanation in the footnote. That is fixed now.
|
1

First of all, all that standard stuff about reading some good books on C++ applies.

In C++, structs ARE classes, the only difference is the default visibility of class members AND that no typedef is necessary for these objects, myStruct is automatically useable as if you typedef'd it. Thus you would have:

struct myStruct {
    int one;
    int two;
};

and you can add member functions and all that on top of this. Plain Old Data (POD) structs are also fine in C++.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.