C++: convert not from obj1 to obj2, but from obj1* to obj2? - c++11

A constructor for MyClass takes a pointer to another such object.
The C++ MyClass is functionally the same as a C "class" based on a typedef'd struct called MyType_T. (The C++ class is basically a wrapper to the old C code.) I'd like to be able to pass in a MyClass* anywhere I could pass in a MyType_T* before.
I'd like to write an automatic conversion of any MyClass* to MyType_T*, but I guess what's throwing me is that my type converter is written to take a MyClass not a MyClass*. Even though I'm sure that's the problem, I can't think of what syntax would solve it. I've thought about making a friend implementation of the cast, but I can't put it before the definition of class MyClass because it won't know the offset of thing. And I can't put after the definition of class MyClass because the MyClass constructor wants to use that conversion.
typedef struct MyStruct {
int iFoo;
struct MyType* ptypeParent;
} MyType_T;
void MyTypeCreator( MyType_T* ptypeSelf, int iFoo_in, MyType_T* ptypeParent );
class MyClass {
public:
MyClass( int iFoo, MyClass* pclassParent ) {
MyTypeCreator( &thing, iFoo, pclassParent ); <--------------- PROBLEM
MyTypeCreator( &thing, iFoo, &pclassParent->thing ); <------- WORKS
};
operator MyType_T*() { return &thing; } <---------------- INCORRECT: attempts to convert MyClass, not MyClass*, to MyType_T*.
MyType_T thing;
};
QUESTION 1: how to write a convertor from MyClass* instead of MyClass?
QUESTION 2: how can such a convertor check for NULL input? (If thing isn't offset of 0, but say 8, then converting from a NULL pclass without a check would give a value of 0x00000008, not NULL...)

Related

Derived class initialization with parameter of another derived class

I am new in Abstract classes so please excuse any ignorant mistakes
The exercise is given from my school, so the main.cpp file is to be used, almost as it is
I am trying to create a simple calculator in Eclipse using C++11
There exists a simple Abstract class with two virtual methods.
The two derived classes are simply the "Result" and the "Const" classes.
This is the header file of the Abstract class called
Expression.h
class Expression
{
public:
Expression();
virtual ~Expression();
//methods
};
Following is the source file of Expression
Expression.cpp
#include "expression.h"
#include <iostream>
Expression::Expression(){}
Expression::~Expression(){}
Then I have created two classes called Const and Result
Const.h
#include <iostream>
#include "expression.h"
class Const : public Expression
{
public:
Const(int value);
//inherited methods
private:
int value;
};
and the source file
Const.cpp
#include "expression.h"
#include "Const.h"
Const::Const(int x)
{
value=x;
};
//inherited methods
Result.h
#include <iostream>
#include "expression.h"
#include "Const.h"
class Result : public Expression
{
public:
Result(Const& c);
//inherited methods
private:
double value;
};
Result.cpp
#include "expression.h"
#include "Result.h"
Result::Result(Const& c)
{
value=c.point;
};
//inherited methods
So what i need is to understand
main.cpp
#include <iostream>
#include "expression.h"
#include "const.h"
#include "result.h"
void testResult()
{
Result res (new Const(4));
//Here an inherited method will be used to print the contents of object res
}
int main()
{
testResult();
return 0;
}
The problem i can't solve is the line
Result res (new Const(4));
The error i get is
Conversion from 'Const* to non-scalar type 'Result' requested
The thing is that what is described in this line should be used as it is, and i can't seem to find exactly what it is.
EDIT
The question as asked firstly was apparently misleading due to my fault, tried to fix the question so as to describe exactly my problem
You started correctly, by creating a common base class for both Const and Result, but then completely ignored it.
All your problems are indeed in this line:
Result res = (new Const(4));
First of all, operator new in C++ returns a pointer, not a reference. Also, this would be a good place to make use of your base class:
Expression* res = new Const(4);
Since you declared methods evaluate() and print() as virtual, the object res is pointing to will be correctly resolved as an instance of Const when you call res->print() or res->evaluate().
This will use Const version of print(). If you want to use the Result version - abstract classes won't help you here, you need to use casting. Create your own operator=(Const &) in Result or operator Result() in Const.
If you had two derived classes DerivedClass1 and DerivedClass2 both derived from some BaseClass then to instantiate a pointer to a DerivedClass1 and use it in polymorphic way use:
BaseClass* p = new DerivedClass1;
To create a base class pointer to a DerivedClass2 use:
BaseClass* p = new DerivedClass2;
And not the:
DerivedClass1* p = new DerivedClass2;
In your example both Result and Const classes are derived from Expression class hence the confusion. To pass in an argument of SomeClass& type, create a temporary object of SomeClass and pass it in:
SomeClass o;
someFunction(o);
One issue is that
double point= value;
doesn't initialize your member, but a new unused local variable.
just do
point = value;
Class Result and Const are two different classes. This means the type conversion
Result *res = (new Const(4));
is not possible. To do what you want, since both classes inherit from Expression, do:
Expression * res = new Result( objConst);
Where objConst is a Const object.

Should this simple structure have an implicit move constructor?

In this test:
#include <string>
struct thing {
std::string name_;
};
class test {
thing id_;
public:
test(thing id) : id_{std::move(id)} {}
};
I would expect struct thing to have an implicit move constructor, so that class test can use std::move() to initialise its data member.
Clang version 3.4.1 gives this error:
error: no viable conversion from 'typename remove_reference<thing&>::type' (aka 'thing') to 'std::string' (aka 'basic_string<char, char_traits<char>, allocator<char> >')
The problem can be solved by adding a move constructor to struct thing, which of course means that a converting constructor and an explicitly defaulted copy constructor also needs to be added.
I do not understand why I cannot move struct thing around implicitly.
You are using a brace initialization - id_{std::move(id)}. In your case, as though struct thing is a POD (plain old data) it means, that C++ compiler tries to initialize the first member - std::string name_ instead of using a default constructor of struct thing object. Read more about aggregates and PODs.
In this case, because of the braces, class test's constructor is equivalent to something like this:
class test {
thing id_;
public:
test(thing id) {
id_.name_ = std::move(id); // Notice that this causes
// the "no viable conversion" error
}
};
Solution 1: You need to declare explicitly that you want to use a default struct thing's constructor by using parenthesis instead of braces:
#include <string>
struct thing {
std::string name_;
};
class test {
thing id_;
public:
test(thing id) : id_(std::move(id)) {} // Default c-tor will be used
};
Solution 2: You could also declare a user-defined constructor of struct thing to make it non-POD:
#include <string>
struct thing {
std::string name_;
thing(thing&&) {} // Used-defined move constructor
};
class test {
thing id_;
public:
test(thing id) : id_{std::move(id)} {} // You can use braces again
};

C++/CLI - Error C2664 again

I am dealing with following problem. To be formal I am using VS2010 Ultimate and I try to write an windows forms application, but I get specified error:
1>f:\baza danych\baza\baza\Form5.h(475): error C2664: 'Bazadanych::Dodaj1' : cannot convert parameter 1 from 'Car' to 'Car'
1> Cannot copy construct class 'Car' due to ambiguous copy constructors or no available copy constructor
and here are Car.h where I have declaration of this class
public ref class Car
{
public:
String^ category;
String^ model;
String^ rocznik;
String^ cena;
Car(){};
Car(String^ ,String^ ,String^ );
void edytuj(String^ ,String^ ,String^ );
String^ getmodel(){return this->model;};
String^ getrocznik(){return this->rocznik;};
String^ getcena(){return this->cena;};
virtual String^ getcat()
{
this->category="To rent";
return this->category;
};`
}
Definition:
Car::Car(String^ model1,String^ rocznik1,String^ cena1)
{
this->model=model1;
this->rocznik=rocznik1;
this->cena=cena1;
};
void Car::edytuj(String^ model1,String^ rocznik1,String^ cena1)
{
this->model=model1;
this->rocznik=rocznik1;
this->cena=cena1;
};
Declaration of class where method mentioned in error is:
public ref class Bazadanych
{
public:
cliext::list<Car^> Bazatorent;
cliext::list<Rented^> Bazarented;
cliext::list<Unavaible^> Bazaunavaible;
cliext::list<Car^>::iterator it1;
cliext::list<Rented^>::iterator it2;
cliext::list<Unavaible^>::iterator it3;
Bazadanych()
{
it1=Bazatorent.begin();
it2=Bazarented.begin();
it3=Bazaunavaible.begin();
};
bool Empty();
void Dodaj1(Car);
void Dodaj2(Rented);
void Dodaj3(Unavaible);
void Usun1(Car);
void Usun2(Rented);
void Usun3(Unavaible);
void Czysc();
};
and definition:
void Bazadanych::Dodaj1(Car Element)
{
this->Bazatorent.push_back(Element);
};
I have definitions and declarations in separatly .h and .cpp files. For other methods "Dodaj" and "Usun" I have exactly the same problems. If it could help the class Car is base class for class Rented and Unavaible.
I am pretty new in C++/CLI, so I will be very grateful if someone could help me.
I find the error message strange given that it's a managed class. But you can solve it by changing the method's signature to:
void Bazadanych::Dodaj1(Car^ Element) // notice the "^"
Same for the other similar methods.
I'm guessing that without the hat (^), the compiler treats the variable as a regular C++ class, and therefore requires a copy constructor for it, even though managed classes don't even have copy constructors (you can write them but they're never called implicitly like for regular C++ classes).
EDIT: About the error in your comment: Instead of instantiating the class like this:
Car car;
Do it like this:
Car^ car = gcnew Car();
It says what it means: you have no copy constructor for Car. It might look like this:
Car::Car(const Car& c) {
/* your code here*/
};
Some background here and here.

How to marshal following native C++ objects to C++/CLI?

I am a bit confused on how to marshal below mentioned C++ object to c++/CLI. Could you give me some idea?
Native C++ Classes
class HeaderMessage {
double timestamp;
string ric_code;
}
class TradeMessage {
double price;
int size;
}
class RFARecord
{
public:
RFARecord();
HeaderMessage * hMsg;
list<TradeMessage *> qMsgs;
};
My C++/CLI classes look like this
public ref class RFARecordRef
{
public:
RFARecordRef();
RFARecordRef(RFARecord *record);
HeaderMessageRef hMsg;
List<TradeMessageRef> tMsgs;
private:
RFARecord *record;
};
ref class HeaderMessageRef
{
private:
HeaderMessage *hMsg;
public:
HeaderMessageRef(void);
};
ref class TradeMessageRef
{
private:
TradeMessage *tMsg;
public:
TradeMessageRef(void);
};
I am not sure if my approach is correct.
I read data from a text file and transfer this data in the form of RFARecords to my C# program.
What is the right way to wrap or marshal above data objects to C++/CLI which can then be consumed by my C# program.
Thanks in advance.
Regards,
Alok
If I understand correctly. Your biggest task is going to be marshaling the strings from c++ strings into System::String^ objects.
What your going to do is declare a method in the C++/CLI class that returns a type System::String^ like so:
System::String^ get_str_from_cpp()
{
std::string str = ptr_to_native_cpp_class->get_str();
System::String^ ret_str = std_str2sys_str(str);
return ret_str;
}
The std_str2sys_str method looks like so.
static System::String^ std_str2sys_str(std::string std_str)
{
System::String^ sys_str = gcnew System::String(std_str.c_str());
return sys_str;
}
Of course you could use a char* to if you wanted to.
The "ptr_to_native_cpp_class" variable should be a class variable that points to an instance of your native c++ class. It appears you already have those.
There are also ways to marshal from the System::String^ to std::string or char* which are on the net. Hopefully this example helps though. You don't have to worry about marshalling basic types like ints or bools though, you can just return them directly from your C++/CLI layer to the C#.
To answer the question of marshalling an structure over to C++/CLI; I don't think there is a way to automatically marshal an entire custom structure, even if it is composed completely of basic types. What I do in my code is just write a wrapper object that has specific get methods for each data member like so:
//Native C++ class
class data_container
{
public:
int var1;
int var2;
}
//C++/CLI class
public ref class cli_data_container
{
public:
get_var1() {return data_ptr->var1;}
get_var2() {return data_ptr->var2;}
private:
data_container* data_ptr;
};
If there is an automatic way to do this that would be nice, but we had an intern make these interfaces for us for a few dozen utility classes last summer and they get the job done.

boost bind to a data member callback behavior

Can someone please explain this piece of code?
struct Class {
boost::function<void()> member;
};
Class c;
boost::function<boost::function<void()>()> foo = boost::bind(&Class::member, &c);
boost::function<void()> bar = boost::bind(&Class::member, &c);
Why does the definition of bar compile and what is the result of it?
Edit: foo() works as expected, calling c.member(), but bar() doesn't.
The first call is used to "generate" an extractor functor. That functor, when called, will return the member that it was bound to.
The second call just hides the return type of the functor that is passed in (which is the same as in the first example). So essentially, calling bar will do nothing.
You would need to bind if your class was like that:
class Class {
public:
void member();
};
Then what you want to do is that :
Class c;
boost::function<void()> the_function_i_want_to_call = boost::bind(&Class::member, c);
the_function_i_want_to_call.call();

Resources