Local abstract class gives warning C4101 - visual-c++

I have to deal with code that can be reduced as follows:
void main ()
{
class MyBase
{
public:
virtual void MyFunc () = 0;
};
class MyClass : public MyBase
{
public:
virtual void MyFunc () {}
};
MyClass x;
}
When compiling under Visual-C++ 2010 with `cl /nologo /Wall text.cxx', I get the following warning:
test.cxx(7) : warning C4101: 'main::MyBase::MyFunc' : unreferenced local variable
I want to keep MyBase in place if possible (because it is deep down inside the code hierarchy).
Is there a way to 'fix' the code in place (other than using #pragma to ignore the warning)? Or is it bad practice to have a local abstract class?
Thanks in advance!

Related

ACE C++ Library class ACE_TASK doesn't have any activate function?

I have seen how to launch threads in my project, where we are using ACE library . But I have a question when ACE_TASK doesn't have any activate function how can you call it in class derived from MyClass class . The activate function is in ACE_TASK_BASE class which is derived from ACE_TASK . But MyClass class and ACE_TASK_BASE doesn't have any direct relation ?
class MyClass: public ACE_Task< ACE_MT_SYNCH > {
public:
//derived from ACE_Task
virtual int open( void *arg = NULL );
//derived from ACE_Task
virtual int svc();
};
//then we are calling
this->activate(); // ?????
in open(); //running our job in
int svc() {
while( _running )
....
}
You described how activate() is available in MyClass - it inherits from ACE_Task which inherits from ACE_Task_Base. MyClass has access to all the public and protected methods in the inheritance chain.

Calling Method/Function outside a Class but on the same namespace in c++/cli

I have a very simple and yet complicated (atleast for me) question on how to call a method/function outside a class but on a same namespace in c++/cli.
I know that you need to create an instance of an object before you can call a method which is inside a class, something like:
namespace Cars {
public ref class MyClass
{
void Honda(int i)
{
//some code
}
}
void Register()
{
MyClass c;
c.Honda(1);
//some code
}
}
But how do I do the opposite? Like how do I call Register() inside the MyClass::Honda function if they are on the same namespace but not on the same class?
I tried Cars::Register() but it gives an error saying that:
Register() is not a member of "Cars".
Edit: I added the actual code that I tried to access the Register() method.
namespace Cars {
public ref class MyClass
{
void Honda(int i)
{
Cars::Register();
}
}
void Register()
{
//some code
}
}
The line Cars::Register(); do not give any error when I save but when I try to rebuild my application it gives the error below:
Error C2039 'Register': is not a member of 'Cars'
Error C3861 'Register': identifier not found
Just to note that when I put Register() inside the MyClass, everything works well (for some reason I just need to put it outside the class)
Thanks!
There are 2 issues in your code:
Missing ; at the end of the definition for ref class MyClass.
Register() should be defined (or at least declared) before calling it.
Fixed version:
namespace Cars
{
// Defintion:
void Register()
{
//some code
}
public ref class MyClass
{
void Honda(int i)
{
Cars::Register();
}
};
}
Or alternatively:
namespace Cars
{
// Declaration:
void Register();
public ref class MyClass
{
void Honda(int i)
{
Cars::Register();
}
};
// Definition:
void Register()
{
//some code
}
}
Note: since you call Register within the same namespace, you can actually drop the Cars:: qualifier, i.e. simply call: Register();. You also keep it of course, if you think it improves readability.

QtConcurrent::run with a virtual class member

So I'm trying to encapsulate a timer class which will handle all of the gory details of multi-threading and timers.
Here's my code:
TimedEvent.h
class TimedEvent : public QObject
{
Q_OBJECT
public:
explicit TimedEvent(QObject *parent = 0);
TimedEvent(const int intervalInMsecs);
virtual void TimeoutWorkProcedure() = 0;
private slots:
void TimeoutWorkThread();
protected:
QTimer *myTimer;
};
TimedEvent.cpp
TimedEvent::TimedEvent(QObject *parent) :
QObject(parent)
{
}
TimedEvent::TimedEvent(const int intervalInMsecs)
{
// Create timer
//
myTimer = new QTimer(this);
// Connect the timeout signal to our virtual callback function
//
connect(myTimer, SIGNAL(timeout()), this, SLOT(TimeoutWorkThread()));
myTimer->start(intervalInMsecs);
}
void TimedEvent::TimeoutWorkThread()
{
QtConcurrent::run(this, &TimedEvent::TimeoutWorkProcedure());
}
The idea was TimedEvent would be a base class and I would be able to create derived classes very easily.
class MyClass : public TimedEvent
{
public:
MyClass( const int timeoutInMsecs );
TimeoutWorkProcedure(){ do some background stuff };
};
The problem is I cannot figure out what to pass to the QtConcurrent::run call. Not sure this is even possible. I could move the QTConcurrent::run call to the derived class, but I anticipate there being several of these derived classes.
Any ideas would be appreciated.
K.
This code:
void TimedEvent::TimeoutWorkThread()
{
QtConcurrent::run(this, &TimedEvent::TimeoutWorkProcedure);
}
is perfectly fine and will do what you expect. It will call an overridden version of TimeoutWorkProcedure.

can't take address of function unless createing delegate instance

I have a class defined as below:
ref class myClass
{
PictureBox^ pic2;
public:
void setPic2() { pic2 = gcnew PictureBox; }
template<typename UnaryOperator>
void setPic2Click(Form^ x, UnaryOperator op) { pic2->Click += gcnew EventHandler(x, op); }
};
And in my Windows form class:
namespace testProject
{
public ref class Form1 : public System::Windows::Forms::Form
{
void Form1_Load(Object^ sender, EventArgs^ e)
{
rect1.setPic2();
rect1.setPic2Click(this, std::bind1st(std::mem_fun(&Form1::pic2_Click), this));
}
void pic2_Click(Object^ sender, EventArgs^ e)
{
// do something...
}
When compiled, it generated this error which is related to the rect1.setPic2Click call...:
error C3374: can't take address of 'testProject::Form1::pic2_Click' unless creating delegate instance
Basically, I tried to encapsulate the interface of the picturebox by create the instance method setPic2Click. Is this the right approach? Any suggestion how to remedy this error?
Your only mistake is that you're trying to mix managed and unmanaged C++/CLI code in a way that doesn't work (and doesn't make sense).
.NET delegates already have a bound first parameter. All you need is:
class1->setPic2Click(gcnew System::EventHandler(this, &Form1::pic2_Click));
and
void setPic2Click(System::EventHandler^ op) {pic2->Click += op;}

Class members of a native object accessed using JNI change value unexpectedly

I have a Java project in which I use some C++ code using JNI.
I encountered a weird problem.
I have a class that looks more or less like so:
class MyClass
{
private:
MyType* _p;
public:
MyClass();
virtual ~MyClass();
void myFunc();
};
And:
MyClass::MyClass() : _p(NULL) {
// _p's value here is indeed NULL (0)
}
MyClass::~MyClass() {
}
void MyClass::myFunc() {
if (_p != NULL) {
delete _p;
}
_p = new MyType();
}
No other function but myFunc touches _p, and for some reason, even after initializing it to NULL, when calling myFunc for the first time, _p has some garbage value in it and the function attempts to delete it.
The ctor of MyClass is called using JNI, and myFunc is too called using JNI, on a separate occasion.
Any help would be greatly appreciated.

Resources