C++/CLI classes and IList - json

I'm trying to deserialize the following JSON using C++/CLI
[
{
"id":"046e075ad92684",
"NfcA":{
"maxTransceiveLength":253,
"sak":0,
"atqa":"4400",
"timeout":618
},
"Ndef":[
{
"records":[
{
"id":"",
"tnf":1,
"type":"54",
"payload":"02656e48656c6c6f206d792041737365742049442069733a20303030303031"
}
]
}
],
"tech":[
"android.nfc.tech.NfcA",
"android.nfc.tech.MifareUltralight",
"android.nfc.tech.Ndef"
],
"time":1472468356002
}
]
I have already declared the following classes in order to get the contents of JSON data.
ref class tech {
public: String^ tech1;
public: String^ tech2;
public: String^ tech3;
};
ref class Record {
public: String^ id;
public: int tnf;
public: String^ type;
public: String^ payload;
};
ref class Topic_nfc {
public: String^ id;
public: ref class NfcA {
public: int maxTransceiveLength;
public: int sak;
public: int atqa;
public: int timeout;
};
public: ref class Ndef {
public: System::Collections::Generic::IList<Record^>^ records;
};
public: System::Collections::Generic::IList<Ndef^>^ Ndef;
public: System::Collections::Generic::IList<String^>^ tech;
public: unsigned long long time;
public:
NfcA^ NfcA;
};
After deserialization I can access id and maxTransceiveLength normally using
printf("MyRawdata[i]->id : %s\n", MyRawdata[i]->id);
printf("MyRawdata[i]->NfcA->maxTransceiveLength : %d\n", MyRawdata[i]->NfcA->maxTransceiveLength);
where MyRawdata derived from
System::Collections::Generic::IList<Topic_nfc^>^ MyRawdata = JsonConvert::DeserializeObject<System::Collections::Generic::IList<Topic_nfc^>^>(MyJson);
However, I can not figure out how we can access Ndef and tech data members such as payload. Can you indicate which is the equivelant for IList ?
Thank you

Thanks to #Lucas Trzesniewski help, I concluded that these lists can be accessed using the following code.
for (int k = 0 ; k < MyRawdata[i]->Ndef->Count ; k++) {
printf("MyRawdata[i]->Ndef[k]->records[k]->payload : %s\n", MyRawdata[i]->Ndef[k]->records[k]->payload);
}
for (int k = 0 ; k < MyRawdata[i]->tech->Count ; k++) {
printf("MyRawdata[i]->tech[k] : %s\n", MyRawdata[i]->tech[k]);
}
Actually, it was a matter of syntax to access the data in the respective classes.

Related

Use member function as template argument to create a static wrapper

I'm trying to write a c++11 wrapper around a C API, and basically there is a way to register notifications with a static function pointer, which also passes me back an "opaque" pointer, which are provided at a later point, basically a pointer to classes I create, in this example the class foo. Basically, I'm trying to create a static function `helper<..>::call that has the API's signature, but generates code to call my member function on the instance that the c++ wrapper created, and is passed in through an "opaque" pointer along with it. This static function then also converts the arguments when finally calling the member function.
I seem to have this almost working, but I'm having trouble creating a "nicer" public function register_handler in this example, which hides the "uglier" internals. This is the error I'm getting:
test.cpp:154:37: error: no matching function for call to ‘register_handler<&foo::bar>(const char [6])’
154 | register_handler<&foo::bar>("test2"); // <-- trying to wrap it into a function so I can use only one template argument
| ^
test.cpp:137:6: note: candidate: ‘template<class T, class R, class ... Args, R (T::* Func)(Args ...)> void register_handler(const char*)’
137 | void register_handler(const char* name)
| ^~~~~~~~~~~~~~~~
This is my test code:
#include <iostream>
#include <memory>
#include <vector>
#include <map>
#include <cassert>
// inspired by https://stackoverflow.com/a/7943765/2129246
template <typename T>
struct func_traits:
public func_traits<decltype(&T::operator())>
{
};
template <typename R, typename... Args>
struct func_traits<R(*)(Args...)>
{
enum { arity = sizeof...(Args) };
typedef R result_type;
using all_args = std::tuple<Args...>;
template <size_t i>
struct arg
{
typedef typename std::tuple_element<i, std::tuple<Args...>>::type type;
};
};
template <typename C, typename R, typename... Args>
struct func_traits<R(C::*)(Args...) const>
{
enum { arity = sizeof...(Args) };
typedef C class_type;
typedef R result_type;
using all_args = std::tuple<Args...>;
template <size_t i>
struct arg
{
typedef typename std::tuple_element<i, std::tuple<Args...>>::type type;
};
};
template< std::size_t... Ns >
struct indices {
typedef indices< Ns..., sizeof...( Ns ) > next;
};
template< std::size_t N >
struct make_indices {
typedef typename make_indices< N - 1 >::type::next type;
};
template<>
struct make_indices< 0 > {
typedef indices<> type;
};
struct value
{
std::string str_;
template <typename T>
value(T val):
str_(std::to_string(val))
{
}
value(const char* str):
str_(str)
{
}
value(const std::string& str):
str_(str)
{
}
operator int() const
{
return std::stoi(str_);
}
operator double() const
{
return std::stof(str_);
}
operator std::string() const
{
return str_;
}
};
std::map<std::string, void(*)(void*, const std::vector<value>&)> g_handlers;
template <typename T, T>
struct helper;
template <typename T, typename R, typename... Args, R(T::*Func)(Args...)>
struct helper<R(T::*)(Args...), Func>
{
template <size_t... Is>
static void expand(T* obj, const std::vector<value>& args, indices<Is...>)
{
assert(sizeof...(Is) <= args.size());
(obj->*Func)((args[Is])...);
}
static void call(void *p, const std::vector<value>& args)
{
T* obj = reinterpret_cast<T*>(p);
expand(obj, args, typename make_indices<sizeof...(Args)>::type());
}
static void reg_handler(const char* name)
{
g_handlers.insert(std::make_pair(name, call));
};
};
template <typename Obj>
void call_handler(Obj& obj, const char* name, const std::vector<value>& args)
{
auto it = g_handlers.find(name);
if (it != g_handlers.end())
it->second(reinterpret_cast<void*>(&obj), args);
else
std::cout << "handler not registered: " << name << std::endl;
}
// The code below somehow doesn't ever match this template
template <typename T, typename R, typename... Args, R(T::*Func)(Args...)>
void register_handler(const char* name)
{
helper<R(T::*)(Args...), Func>::reg_handler(name);
}
struct foo
{
void bar(int v, const std::string& str, double f)
{
std::cout << "bar: v=" << v << " str=" << str << " f=" << f << std::endl;
};
};
int main()
{
// register member function handlers before we have any instances
helper<decltype(&foo::bar), &foo::bar>::reg_handler("test"); // <-- works, but "ugly" and exposes internal implementation
register_handler<&foo::bar>("test2"); // <-- trying to wrap it into a function so I can use only one template argument
// now we have an instance
foo f;
// call the previously registered handler
call_handler(f, "test", {1, "2", 3.45});
call_handler(f, "test2", {1, "2", 3.45});
return 0;
}
The simple answer for C++11 is: You can't!
From C++17 you are able to use auto also for non type template parameters as a function pointer or member function pointer is not a type here and you have no syntax to describe your function pointer type.
In C++17 you can use it like this:
struct foo
{
void bar(){}
};
template <typename T, T>
struct helper;
template <typename T, typename R, typename... Args, R(T::*Func)(Args...)>
struct helper<R(T::*)(Args...), Func>
{
static void reg_handler(const char* name)
{
// ... here your code continues
}
};
template < auto T >
struct X
{
};
template <typename T, typename R, typename... Args, R(T::*Func)(Args...)>
struct X<Func>
{
static void register_handler( const char* name )
{
helper<R(T::*)(Args...), Func>::reg_handler(name);
}
};
int main()
{
X<&foo::bar>::register_handler("check");
}

Serialize and deserialize objects in C ++

I'm new to C ++, I can not find a detailed example of how to serialize an object in binary so I can save it to the mysql server and then be deserialized.
The object looks like this:
class house{
public:
int houseNumber;
bool isEmpty;
Dweller *dweller;
};
class Dweller{
public:
int phoneNumber;
bool man;
Backpack backpack;
};
class Backpack{
public:
int item1;
int item2;
int item3;
int item4;
float weight;
bool empty;
};
I have to serialize the house object, which owns the other objects.
I was able to solve the problem using:
#include <boost/serialization/serialization.hpp>
#include <boost/serialization/vector.hpp>
See I used #include "boost/serialization/vector.hpp" because now the 'dweller' are in a vector to make things easier
Then the code stayed like this:
class house{
public:
int houseNumber;
bool isEmpty;
std::vector<Dweller> dweller;
private:
friend class boost::serialization::access;
template <class archive>
void serialize(archive &ar, const unsigned int version)
{
ar &houseNumber;
ar &isEmpty;
ar &dweller;
}
};
class Dweller{
public:
int phoneNumber;
bool man;
Backpack backpack;
private:
friend class boost::serialization::access;
template <class archive>
void serialize(archive &ar, const unsigned int version)
{
ar &phoneNumber;
ar &man;
ar &backpack;
}
};
class Backpack{
public:
int item1;
int item2;
int item3;
int item4;
float weight;
bool empty;
private:
friend class boost::serialization::access;
template <class archive>
void serialize(archive &ar, const unsigned int version)
{
ar &item1;
ar &item2;
ar &item3;
ar &item4;
ar &weight;
ar ∅
}
};
So to serialize I used:
#include <iostream>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/serialization/serialization.hpp>
#include <sstream>
int main()
{
Backpack _backpack;
_backpack.item1 = 0;
_backpack.item1 = 1;
_backpack.item1 = 2;
_backpack.item1 = 3;
_backpack.item1 = 4;
_backpack.weight = 3.21;
_backpack.empty = false;
Dweller _dweller1;
_dweller.phoneNumber = 1234567;
_dweller.man = false;
_dweller.backpack = _backpack;
Dweller _dweller2;
_dweller.phoneNumber = 7654321;
_dweller.man = true;
_dweller.backpack = _backpack;
std::vector<Dweller> _dwellers;
_dwellers.push_back(_dweller1);
_dwellers.push_back(_dweller2);
house _house;
_house.houseNumber = 4;
_house.isEmpty = false;
_house.dweller = _dwellers;
/*Serializing*/
std::stringstream ss;
boost::archive::binary_oarchive oa(ss);
oa << _house;
/*Deserializing*/
boost::archive::binary_iarchive ia(ss);
house _house2;
ia >> _house2;
}

Passing class function using function pointer to external library

I have a class that uses a preexisting library. There is a function call that needs a function pointer, and I am trying to pass in the function that is in my class. It doesn't compile though. What can I do to fix this? (Also, I'm sure this was asked before in a much clearer way. I'm out of my element with this, so my apologies).
Note: This is for an arduino.
In my main program I have the following code...
#include "CM.h"
CM cm;
void setup() {
cm.setup();
}
CM.h
class CM {
private:
LibClass *lib;
void onInit();
public:
void setup();
};
CM.cpp
#include "CM.h"
void CM::setup() {
lib->attach(onInit); // <-- this isn't working.
}
void CM::onInit() {
Serial.println("HERE I AM");
}
To pass a member function, you need to make it "static" and then pass it with a full scope qualifier:
#include <iostream>
void attach( void (*func)(void) );
class CM {
private:
static void onInit();
public:
void setup();
};
void CM::setup()
{
attach(CM::onInit);
}
void CM::onInit(void)
{
std::cout << "HERE I AM";
}
// a global function pointer for this example
void (*p_func)(void);
// a "library" attach function
void attach( void (*func)(void) )
{
p_func = func;
}
int main(int argc, const char * argv[]) {
CM my;
my.setup();
p_func(); // like the library call
return 0;
}

Qt fetched database record field values are empty, the data EXISTS in database

I have Qt/QML app that connects to database and now I would like to fetch data through sublcassed QSqlQueryModel:
#ifndef UEPEOPLEMODEL_H
#define UEPEOPLEMODEL_H
#include <QImage>
#include <QVariant>
#include <QStringList>
#include <QDebug>
#include <QHash>
#include <QByteArray>
#include <QSqlError>
#include <QSqlQueryModel>
#include <QSqlRecord>
#include <QModelIndex>
#include <QQuickImageProvider>
#include <QByteArray>
#include <QSqlRecord>
#include <QDebug>
#include <QSqlQuery>
#include "../settings/uedefaults.h"
#include "../settings/uetypes.h"
class UePeopleModel : public QSqlQueryModel,
public QQuickImageProvider
{
Q_OBJECT
private:
QSqlDatabase m_ueDb;
private:
QSqlDatabase ueDatabase() const
{ return this->m_ueDb; }
void ueSetDatabase(const QSqlDatabase& database)
{ this->m_ueDb=database; }
public:
UePeopleModel(QObject *parent=0);
~UePeopleModel();
QVariant data(const QModelIndex &index,
int role) const Q_DECL_OVERRIDE;
QImage ueImage(const QString &id) const;
QImage requestImage(const QString &id,
QSize *size,
const QSize &requestedSize);
UeTypeRoles roleNames() const;
public:
static const int ueRoleName=Qt::UserRole+1;
static const int ueRoleImage=Qt::UserRole+2;
};
#endif // UEPEOPLEMODEL_H
and here is implementation:
#include "uepeoplemodel.h"
UePeopleModel::UePeopleModel(QObject* parent)
: QSqlQueryModel(parent),
QQuickImageProvider(QQmlImageProviderBase::Image,
QQmlImageProviderBase::ForceAsynchronousImageLoading)
{
//QSqlDatabase db;
if(!QSqlDatabase::connectionNames().contains(UePosDatabase::UeDatabaseConnectionNames::DATABASE_CONNECTION_NAME_PEOPLE,
Qt::CaseInsensitive))
{
this->ueSetDatabase(QSqlDatabase::addDatabase(UePosDatabase::DATABASE_DRIVER,
UePosDatabase::UeDatabaseConnectionNames::DATABASE_CONNECTION_NAME_PEOPLE));
} // if
this->ueDatabase().setHostName(/*this->uePosSettings()->ueDbHostname()*/UePosDatabase::UeDatabaseConnectionParameters::DATABASE_HOSTNAME);
this->ueDatabase().setDatabaseName(/*this->uePosSettings()->ueDbName()*/UePosDatabase::UeDatabaseConnectionParameters::DATABASE_NAME);
this->ueDatabase().setUserName(/*this->uePosSettings()->ueDbUser()*/UePosDatabase::UeDatabaseConnectionParameters::DATABASE_USERNAME);
this->ueDatabase().setPassword(/*this->uePosSettings()->ueDbPassword()*/UePosDatabase::UeDatabaseConnectionParameters::DATABASE_PASSWORD);
if(this->ueDatabase().open())
{
this->setQuery(UePosDatabase::UeSqlQueries::UeTablePeople::SQL_QUERY_GET_ALL_PEOPLE,
this->ueDatabase());
//qDebug() << this->ueDatabase().lastError().text();
}
else
{
qDebug() << this->ueDatabase().lastError().text();
}
} // default constructor
UePeopleModel::~UePeopleModel()
{
} // default destructor
QVariant UePeopleModel::data(const QModelIndex &index,
int role) const
{
QVariant value=QVariant();
switch(role)
{
case ueRoleImage:
{
value=this->record(index.row()).value(UePosDatabase::UeTableIndexes::UeTablePeople::INDEX_IMAGE).toByteArray();
break;
} // case
case ueRoleName:
{
value=this->record(index.row()).value(UePosDatabase::UeTableIndexes::UeTablePeople::INDEX_NAME).toString();
break;
} // case
default:
return value;
} // switch
return QSqlQueryModel::data(index,
role);//value;
} // data
QImage UePeopleModel::ueImage(const QString &id) const
{
return QImage::fromData(this->record(id.toInt()).value(UePosDatabase::UeTableIndexes::UeTablePeople::INDEX_IMAGE).toByteArray(),
"PNG");
} // image
QImage UePeopleModel::requestImage(const QString &id,
QSize *size,
const QSize &requestedSize)
{
Q_UNUSED(requestedSize);
QImage image=this->ueImage(id);
*size = image.size();
return image;
} // requestImage
UeTypeRoles UePeopleModel::roleNames() const
{
UeTypeRoles roles;
const int iRoleName=UePeopleModel::ueRoleName;
const int iRoleImage=UePeopleModel::ueRoleImage;
roles.insert(iRoleName,
"ueRoleName");
roles.insert(iRoleImage,
"ueRoleImage");
return roles;
} // roleNames
Now, the problem is I get emtpty values from database, and therefore qml outputs errors:
> qrc:/gui/windows/UeKeypad.qml:140:43: Unable to assign [undefined] to
> QString qrc:/gui/windows/UeKeypad.qml:140:43: Unable to assign
> [undefined] to QString qrc:/gui/windows/UeKeypad.qml:118:33: QML
> Image: Failed to get image from provider:
> image://uepeoplemodel/undefined qrc:/gui/windows/UeKeypad.qml:118:33:
> QML Image: Failed to get image from provider:
> image://uepeoplemodel/undefined qrc:/gui/windows/UeKeypad.qml:140:43:
> Unable to assign [undefined] to QString
> qrc:/gui/windows/UeKeypad.qml:140:43: Unable to assign [undefined] to
> QString qrc:/gui/windows/UeKeypad.qml:140:43: Unable to assign
> [undefined] to QString qrc:/gui/windows/UeKeypad.qml:140:43: Unable to
> assign [undefined] to QString qrc:/gui/windows/UeKeypad.qml:118:33:
> QML Image: Failed to get image from provider:
> image://uepeoplemodel/undefined qrc:/gui/windows/UeKeypad.qml:118:33:
> QML Image: Failed to get image from provider:
> image://uepeoplemodel/undefined qrc:/gui/windows/UeKeypad.qml:118:33:
> QML Image: Failed to get image from provider:
> image://uepeoplemodel/undefined qrc:/gui/windows/UeKeypad.qml:118:33:
> QML Image: Failed to get image from provider:
> image://uepeoplemodel/undefined
Why am I getting empty values from table?!
Hmm, I've been doing research on problem, and in Qt docs it states:
If the model is not initialized, an empty record will be returned.
The link to docs: QSqlQueryModel::record. This might be the problem, however, I initialize UePeopleModel in main.cpp:
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QQmlApplicationEngine engine;
UePeopleModel* uePeopleModel=new UePeopleModel(qApp);
engine.rootContext()->setContextProperty("uePeopleModel",
uePeopleModel);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
What am I still missing? I've checked db connection numerous times, it is ok, I also get field names from table, but no data.
Hmm, I've added debug code to QVariant UePeopleModel::data(const QModelIndex &index, int role) const method:
QVariant UePeopleModel::data(const QModelIndex &index,
int role) const
{
switch(role)
{
case ueRoleImage:
{
return this->record(index.row()).value(UePosDatabase::UeTableIndexes::UeTablePeople::INDEX_IMAGE).toByteArray();
break;
} // case
case ueRoleName:
{
int nrrecords=this->rowCount(QModelIndex());
QString name=this->record(index.row()).value(UePosDatabase::UeTableIndexes::UeTablePeople::INDEX_NAME).toString();;
return this->record(index.row()).value(UePosDatabase::UeTableIndexes::UeTablePeople::INDEX_NAME).toString();
} break; // case
case ueRolePassword:
{
QString password=this->record(index.row()).value(UePosDatabase::UeTableIndexes::UeTablePeople::INDEX_APPPASSWORD).toString();
return this->record(index.row()).value(UePosDatabase::UeTableIndexes::UeTablePeople::INDEX_APPPASSWORD).toString();
} break; // case
default:
return QVariant();
} // switch
return QSqlQueryModel::data(index,
role);//value;
} // data
and after the line int nrrecords=this->rowCount(QModelIndex()); I've set a breakpoint, the temp variable nrrecord holds value 5 - which is excatly number of records in database! Why is then method's parameter index empty?? The data() method is being called from QML file via QML items model property.
There are following problems at least:
Your switch cases all fall through (you must break after each one).
You don't need any accessors to the private members, you are supposed to use them directly. That's why they are private. Accessors are used as encapsulation when you want to isolate the implementation details from public (or protected) interface. Remove the accessors for m_ueDb and use the member directly.
Your implementation of data will only provide values for image roles. You must forward the default role access to the base class's data():
QVariant UePeopleModel::data(const QModelIndex &index, int role) const
{
switch(role) {
case ueRoleImage:
for(int iIndex=0; iIndex<this->record().count(); iIndex++) {
qDebug() << this->record().fieldName(iIndex) <<
<< this->record().value(iIndex) <<
<< index.row();
}
return record(index.row()).value(
UePosDatabase::UeTableIndexes::UeTablePeople::INDEX_IMAGE).toByteArray();
case ueRoleName:
return record(index.row()).value(
UePosDatabase::UeTableIndexes::UeTablePeople::INDEX_NAME).toString();
}
return QSqlQueryModel::data(index, role);
}
You should never use the <QtModule/QtHeader> include style. That you do indicates that your .pro file doesn't declare the use of the sql module and/or that you haven't re-run qmake on your project after adding the module to the project (you should have QT += sql somewhere in your .pro file). Fix as follows:
// CORRECT
#include <QSqlError>
// WRONG
#include <QtSql/QSqlError>

Calling a Base Class Construtor in a function of Derived Class

I am trying to call a constructor of a Base Class in a function of Derived Class. Here is the code:
Classes:
#pragma once
#include "CAR_TYRE_DOOR.h"
#include <string>
using namespace std;
//#ifndef 1_A_H_B
//#define 1_A_H_B
class Honda_Civic: public Car
{
private:
string CNG_y_n;
public:
Honda_Civic();
Honda_Civic(string CNG);
Honda_Civic(Honda_Civic& H1);
void set_CNG_y_n(string S);
string get_CNG_y_n();
void print();
};
class BMW: public Car
{
private:
string conv_y_n;
public:
BMW();
BMW(string S);
BMW(BMW& BMW1);
void set_conv_y_n(string S);
string get_conv_y_n();
void print();
};
class Mercedes: public Car
{
private:
int no_WS;
string SGR_y_n;
public:
Mercedes();
Mercedes(int no_WS, string SGR_y_n);
Mercedes(Mercedes& Merc);
//::Car( Merc1);
void set_no_WS(int n);
void set_SGR(string SGR);
int get_no_WS();
string get_SGR();
void print();
};
//#endif
The BaseClass functions:
//#include "BMW+MERC.h"
#include "CAR_TYRE_DOOR.h"
#include "Honda.h"
#include "S_R.h"
#include <iostream>
#include <string>
using namespace std;
void Car::set_color(string S)
{
S = this->color;
}
void Car::set_model(string S)
{
S = this->model;
}
void Car::set_cost(float x)
{
x = this->cost;
}
string Car::get_color()
{
return this->color;
}
string Car::get_model()
{
return this->model;
}
float Car::get_cost()
{
return this->cost;
}
Car::Car()
{
}
Car::Car(string color, string model, float cost)
{
this->color = "white";
this->model = "2011";
this->cost = 1000000;
}
Car::Car(Car& C1)
{
this->color = C1.color;
this->model = C1.model;
this->cost = C1.cost;
for(int i=0; i<4; i++)
{
DX[i] = C1.DX[i];
}
for(int i=0; i<4; i++)
{
TX[i] = C1.TX[i];
}
}
void Car::print_car()
{
cout <<"Car color: "<<get_color()<<endl;
cout <<"Car model: "<<get_model()<<endl;
cout <<"Car door color: "<<DX[0].get_color()<<endl;
cout <<"Car door vendor: "<<DX[0].get_vendor()<<endl;
cout <<"Tyre vendor: "<<TX[0].get_vendor()<<endl;
for(int i=0; i<4; i++)
{
cout <<"Tyre"<< i+1 <<"type: "<<TX[i].get_rubber_type()<<endl;
}
}
The Derived Class:
#include "Honda.h"
#include <iostream>
#include <string>
using namespace std;
Mercedes::Mercedes()
{
}
Mercedes::Mercedes(int no_WS, string SGR_y_n)
{
this->no_WS = 4;
this->SGR_y_n = "Yes";
}
Mercedes::Mercedes(Mercedes& Merc)
{
Mercedes::Car( Merc);
this->no_WS = Merc.no_WS;
this->SGR_y_n = Merc.SGR_y_n;
}
void Mercedes::set_no_WS(int n)
{
this->no_WS = n;
}
void Mercedes::set_SGR(string SGR)
{
this->SGR_y_n = SGR;
}
int Mercedes::get_no_WS()
{
return this->no_WS;
}
string Mercedes::get_SGR()
{
return this->SGR_y_n;
}
void Mercedes::print()
{
Mercedes.print_car();
cout <<"Number of Woofer Speakers: "<<get_no_WS()<<endl;
cout <<"Sunglass Roof: "<<get_SGR()<<endl;
}
Now in the copy constructor of the derivedclass, i am trying to call the copy constructor of the base class using:
Mercedes::Mercedes(Mercedes& Merc)
{
Mercedes::Car( Merc);
this->no_WS = Merc.no_WS;
this->SGR_y_n = Merc.SGR_y_n;
}
See this: Mercedes::Car( Merc);
to implement this, please tell me the syntax.
The proper way to call constructor hierarchies is like this:
class Car
{
public:
Car () { }
Car (cosnt Car &) {}
};
class Mercedes : public Car
{
public:
Mercedes () { }
Mercedes (const Mercedes &) {}
};
Mercedes :: Mercedes () : Car() { }
Mercedes :: Mercedes (const Mercedes &car) : Car(car) { }
A copy constructor looks like this (notice the const):
Class :: Class (const Class &)