Objective-C with C++ Member Initialization - c++

I have a C++ object declared in my Objective-C class.
The C++ object has only one constructor that takes several parameters.
In C++ code I would parse the parameters to the constructor in the initializer list of the class that contains it.
How do I do the same in Objective-C?
When I compile I get an error telling me that there is no appropriate default constructor for my C++ object, as you would expect.

I don't think you are able to do something like that in Objective-C, because there are no constructors and initializer lists there. But you can use std::shared_ptr or other smart pointer to store a pointer to your class and create the object itself in init method

You could change the object to a (smart) pointer for this object type. Then you can delay the construction of the object to the moment you see fit.

Related

Why the `this` pointer worked in constructor in C++?

I know the this pointer will point to the object instance that currently in, but I don't know how to implement it.
And I found that in standard 7.5.2 said:
The keyword this names a pointer to the object for which an implicit object member function ([class.mfct.non.static]) is invoked or a non-static data member's initializer ([class.mem]) is evaluated.
but in 12.2.2 said constructors do not have implicit object parameter, then why I can use this pointer in constructor?
then I have several question comming, so my question is:
Does the this pointer belongs to the class?
I think the answer is NO, but I wanna confirm it.
Does all class instance shared the constructors and destructor?
I knew that the class instance will share the member function, does it same on constructors and destructor?
How the this pointer was implemented?
Is there something like virtual table to maintain the this pointer?
Why I can use the this pointer in constructor?
The this point to the implicit object, but constructor didn't have the implicit object parameter, then how could the this pointer worked well?
Any additional supplements and recommendations are appreciated.
Edit:
This stackoverflow helps me a lot : Does a constructor also have an implicit this parameter
but I don't know how to implement it.
this pointer is a feature of the C++ language. If you aren't implementing C++, then you don't need to "implement" this pointer. If you want to know how to implement C++, there are open source C++ compilers available.
why I can use this pointer in constructor?
Constructor is a non-static member function. You can use this in non-static member functions.
but in 12.2.2 said constructors do not have implicit object parameter
Quoted rule is prefaced: For the purposes of overload resolution.... It doesn't apply to anything other than overload resolution.
Does the this pointer belongs to the class?
There is no concept of "belongs" in the language.
Does all class instance shared the constructors and destructor?
There is no concept of sharing functions in the language.
How the this pointer was implemented?
The language doesn't describe how to implement itself. It describes how the program written in the language shall or may behave.
Why I can use the this pointer in constructor?
See above.

object instantiation in c++

In C++ is it true that if you instantiate an object of a class, that for every
object all of methods of the class are copied for the new object?
I tried to point to a method of a class with two different objects, but I'd problems
with pointer to member.
Any idea?
In C++ is it true that if you instantiate an object of a class, that for every object all of methods of the class are copied for the new object?
No, member functions are not usually copied anywhere. A different implicit parameter this is instead passed to any non-static member function, for each object of that class-type.
No, that is absolutely not true.
Class instances (objects) contain data members. Function members look like they're "in" the class, but that's only for scoping and such: your function code doesn't "exist" inside the type, and it certainly doesn't exist inside the object†.
† I think it could, theoretically, in that the standard doesn't outright forbid it. But honestly, no. Just no.
The code for a class exists only once.
For getting a pointer to a member function (probably what you meant by method), take a look at std::function, and for attaching the function call to different objects, take a look at std::bind.

Why cant pass-by-value work if the function parameter type is an ABC?

I am reading a book which says:
pass-by-value doesn't work if the function you're sending the argument to has a parameter type which is an abstract base class (ABC). You have to pass the argument (which would be a derived class) by reference.
My questions are:
Why can you not pass the derived class argument by value?
Is there something "internal" to C++ which requires it to be a reference?
Is this to do with the way dynamic binding works?
This is because of object slicing: the abstract class received by value will receive a copy of the object that you are passing; therefore, the derived data members will be sliced off from it, making the object unusable.
When you pass by reference, you are passing the address of the existing object, which is a member of a derived class, all its data members included. This is not specific to C++ - this is a property of passing by value in general.
The dynamic binding is implemented in such a way as to not allow sliced instance receive calls as if it were a complete instance. The vtable pointer is adjusted to dispatch the calls to the member functions of the abstract base class.
Why can you not pass the derived class argument by value?
Simply put, if you have a function that looks like this:
void foo(T t); // Takes a T by value
And you call it this way (whatever v is):
foo(v);
What happens is that the parameter t represents a new object which is copy-initialized from the argument v, as if you were doing:
T t = v;
Now if T is abstract, the above is illegal, because you cannot instantiate an abstract class (by definition). Therefore, the parameter of foo() cannot be copy-initialized from its argument.
Is there something "internal" to C++ which requires it to be a reference?
Something like that. Dynamic polymorphism in C++ can only work through references and pointers. When you try to assign an object of a derived class to an object of a base class, what you get is object slicing.
You can pass a derived class by value. You cannot pass the abstract base class as an argument by value because there is no way to instantiate an abstract class.
In general you want to pass by reference or pointers so you do not slice the object. There is a good answer here: What is object slicing?
When you receive an object by value, its dymanic (its "real" type) type is its static type. It is not possible to have a variable whose dynamic type is an abstract one. No matter how you build it.

Why is Builder pattern better than a Constructor with arguments in the Class's object being created?

Why can we not the different build steps within the constructor itself.
if the build steps take arguments why can't they be provided as arguments to constructor and utilized within constructor to create the object.
AFAIK, in Builder pattern, the client which specific object to create; then what is the advantage in using a builder instead of a Constructor with arguments in the Class's object being created?
Oh! I get it. I was looking at the Wikipedia example and realized why Builder is helpful. It is helpful, when the client does not know which arguments to pass to the constructor as it is very complicated and hence cannot call the constructor directly and get the object. Consequently, he asks for help from the Concrete Builders who know what arguments to pass to constructors and hence get the object created.
Basically, if the client is the one who is mostly going to be passing the arguments to the constructor of the Class whose object is created, then Builder is not that helpful. It is perhaps better to use the prototype. On the other hand, if there is a small finite set of specific objects that can be created from the class by passing arguments to the constructor (or calling setters) to that class and if they are the ones that are frequently used, then it better to encapsulate this argument passing thingy in the Builder class and use them to create the objects for you.

how c++ implements dynamic binding internally?

Respected Sir!
please explain how c++ implements this dynamic binding a pictorial representation would be more useful in understanding perspective.
or suggest a websigt which contains pictorial representations and full detail about this topic.
let me paste some text for you, any vote is appreciated, :O
[20.2] How can C++ achieve dynamic binding yet also static typing?
When you have a pointer to an object, the object may actually be of a class that is derived from the class of the pointer (e.g., a Vehicle* that is actually pointing to a Car object; this is called "polymorphism"). Thus there are two types: the (static) type of the pointer (Vehicle, in this case), and the (dynamic) type of the pointed-to object (Car, in this case).
Static typing means that the legality of a member function invocation is checked at the earliest possible moment: by the compiler at compile time. The compiler uses the static type of the pointer to determine whether the member function invocation is legal. If the type of the pointer can handle the member function, certainly the pointed-to object can handle it as well. E.g., if Vehicle has a certain member function, certainly Car also has that member function since Car is a kind-of Vehicle.
Dynamic binding means that the address of the code in a member function invocation is determined at the last possible moment: based on the dynamic type of the object at run time. It is called "dynamic binding" because the binding to the code that actually gets called is accomplished dynamically (at run time). Dynamic binding is a result of virtual functions.