What would be a good approach to implement a class in c++ like this:
Someclass.h:
class SomeClass
{
public:
SomeClass();
void kill();
}
Someclass.cpp:
SomeClass::kill(){
kill();//This would cause an infinit recursion
//How to fix it?
}
So what I'm trying to do is redeclare a function within my object as a method.
I can't find if there is a namespace or something simular, that contains "kill()", "sleep(int sec)".
Hope you can help.
SomeClass::kill(){
::kill();
}
:: accesses global scope
Related
I'm currently learning nested classes in C++ while building a project and I currently inside setupBLE() I need to pass one of the nested classes but to init that new class I need to pass to its constructor the outer class so it can access its variables and functions but I'm not exactly sure how to pass to the constructor the pointer of the class that's trying to create it.
It's a bit confusing so I hope the code helps with it.
Like in python we have self but in C++ as far as I know we don't have that so I was wondering what should I pass to the constructor.
Code (PillDispenser.h):
class PillDispenser {
public:
explicit PillDispenser(BLEAddress deviceAddress);
private:
BLEAddress _device_address;
BLEAdvertisedDevice _device;
bool _connected;
// Device properties
std::string _device_name;
// Callbacks
static void notifyCallBack();
class AdvertisedDeviceCallBack : public BLEAdvertisedDeviceCallbacks {
PillDispenser &_outer;
explicit AdvertisedDeviceCallBack(PillDispenser &outer) : _outer(outer){};
void onResult(BLEAdvertisedDevice advertisedDevice) override;
};
}
Code (PillDispenser.cpp):
void PillDispenser::setupBLE() {
BLEScan *scanner = BLEDevice::getScan();
scanner->setAdvertisedDeviceCallbacks(new AdvertisedDeviceCallBack());
scanner->setInterval(SCAN_INTERVAL);
scanner->setWindow(SCAN_WINDOW);
scanner->setActiveScan(true);
scanner->start(SCAN_DURATION);
}
Issue:
This line is trying to use the default constructor which does not exist
scanner->setAdvertisedDeviceCallbacks(new AdvertisedDeviceCallBack());
instead you should use the explicit constructor you defined
scanner->setAdvertisedDeviceCallbacks(new AdvertisedDeviceCallBack(*this));
note that this (in this context) has type PillDispenser* so you have to dereference with * to get a PillDispenser&
I am new to TBB. I met a problem, I hope you guys can give me some advise.
A class named Fruit:
Fruit.hpp:
class Fruit {
protected :
void A(...);
void B(..);
public :
Fruit();
~ Fruit();
void l1(....);
void l2(...);
};
Fruit.cpp:
custom_TBB (...){
//How to call the method A of Fruit?
}
class Fruit {
...
Fruit::l1(....){
tbb::task_scheduler_init init(numberOfThreads);
parallel_for(tbb::blocked_range<int>(0,End,10000),custom_TBB (...));
}
...
};
Because I need to use parallel method in the function of l1.
I am not sure whether my understanding of TBB is right to not?
I would like to know how can I make this works?
Thank you first.
This is my idea:
1. I tried to declare custom_TBB inside the class Fruit, and implement it here, but failed.
2. I tried to define custom_TBB inside the function of l1, but also failed.
Ideally you should declare that tbb::task_scheduler_init init(numberOfThreads); as soon as you can in your program. I generally have it as pretty much the first thing in the main() function of anything I do using TBB.
Here's my situation. I have a class in which I have defined a vector publicly, as below:
class TrackObjects
{
public:
vector<MovingObj> movingObjects;
...
etc.
It has a constructor and everything. I have a separate .cpp file with some implementations where I'm trying to use that vector and methods on that vector. As one example, it's part of a condition in a function like so:
if (movingObjects.locX >= x1)
...
etc.
It tells me movingObjects is undeclared, and to first use this function. It's not a function, and to my knowledge, I haven't called it like one/tried to use it like one.
Can anyone suggest why I might be getting this error?
EDIT: locX is a public variable in the another class MovingObj. TrackObj is the class that creates the vector for objects MovingObj creates. Sorry, I really should've specified that. Like so:
class MovingObj {
public:
MovingObj(int inId, int inLocX, int inLocY, int inWidth, int inHeight);
int id, locX, locY,width,height;
Based on what you are telling us, the proper way to access locX would be something along the lines of:
TrackObjects objs;
objs.movingObjects[15].locX = 123.45;
Or, maybe:
if(objs.movingObjects[15].locX >= 15)
{
//do something
}
You can also encapsulate your access method in TrackObjects (put this in your TrackObjects.cpp implementation):
bool TrackObjects::testLocX(int pos)
{
if(movingObjects[pos].locX>=15)
return true;
return false;
};
This is an elementary C++ issue. movingObjects is part of an object. Code that is not part of the TrackObjects class can only access movingObjects by specifying which object's movingObjects you wish to access.
if (someobject.movingObjects.size() > 0)
...
Another issue is that to access such an object from another cpp file you will first have to #include the file that contains the class definition.
i wrote this code but friend function is not working(foodmoney and hobbymoney are not declare in my friend function. where is my Error here ?
#include <iostream>
using namespace std;
class myBase
{
private:
int friendvar;
int foodmoney;
int hobbymoney;
public:
void setdata();
myBase(){friendvar=0;}
friend void caldata(myBase &mbo);
};
void myBase::setdata()
{
cout<<"Enter foodmoney :" ;cin>>foodmoney;
cout<<"enter hoobymoney:";cin>>hobbymoney;
}
void caldata(myBase &mbo)
{
mbo.friendvar=(foodmoney+hobbymoney)/2;
cout<<mbo.friendvar<<endl;
}
int main()
{
myBase baseobj;
baseobj.setdata();
myBase friends;
caldata(friends);
return 0;
}
mbo.friendvar=(foodmoney+hobbymoney);
should be
mbo.friendvar=(mbo.foodmoney+mbo.hobbymoney);
etc. etc.
Friend functions are not member functions, so they do not have special access to any particular object. You must specify which object you wish to access (by using mbo in your case).
Having said that I can't see any good reason why caldata is a friend function. Why not make it a regular member function? Or maybe you should make it a friend function with two arguments? It's hard to say what you're trying to achieve here.
change
cin>>foodmoney;
to
cin>>mbo.foodmoney;
and change
cin>>hobbymoney;
to
cin>>mbo.hobbymoney;
Your code doesn't compile at all. Fix is described already. But to make ti complete:
As you have referenced friendvar using mbo.friendvar, do the same for foodmoney and hobbymoney.
Also you have not initialized foodmoney and hobbymoney in your constructor. And in your call to caldata(friends); you are therefor getting random result printed on screen.
Last: you instantiate new object with myBase friends; so this is completely new object that is not affected by previous call of setdata().
Your low level requirements for this function are not defined in your question, I cannot help more.
Suppose I have a class called Poem.
class Poem{
virtual void recite() = 0;
}
And I have hundreds of .cpp and .hpp files which describe a subclass, like the following
class TheRaven : public Poem{
void recite() { std::cout << "NEVERMORE!" << endl; }
}
And the likes. And in the main function, I'd like to be able to just iterate through every single possible Poem subclasses and call their recite() function. So I made a class:
class PoemRegistry{
std::map<std::string, Poem*> poems;
PoemRegistry& getGlobal(); // returns a static global registry
void register(const std::string& poemname, Poem * thepoem);
};
And then for each poem subclass .cpp file, I put the following.
class TheRavenRegistor{
TheRavenRegistor(){
PoemRegistry::getGlobal().register("theraven", new TheRaven());
}
}
TheRavenRegistor registor();
ninja edit: I put the global class there, forgot about it
Making it easy, I make a shortcut with #define and templates.
Now, the question is, I just heard about the static class initialization fiasco. I suppose this will be immune against it, or is there something I am definitely missing here? Or is there something more elegant that can be used for this purpose?
This is an example for the Singleton design pattern. Don't use a static global, since the initialisation order is undefined across compilation units.
Instead use something like this:
PoemRegistry& PoemRegistry::getGlobal()
{
static PoemRegistry theRegistry; // construction guaranteed when first call
return theRegistry;
}
Make the getGlobal() method static:
class PoemRegistry
{
public:
static PoemRegistry& getGlobal();
...