Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
double GetTime(const CMY * tool)
{
	double d_tAtt;
	double d_tDis;

	switch (tool->GetMode())
	{
	case 0:
		AfxMessageBox(_T("non contemplato"));
		break;
	case 1:
	case 2:
		
	case 3:
	
		break;
	case 4:
		
		break;
	

	}
	return 0.0;
}


in every case I must use a derived class of CMY, but I don't know I can write.

in this way?

CMYdERIV tool1 = CMY tool?

What I have tried:

I searched on internet, I hope I explain well
Posted

You should be able to write:
C++
CMYdERIV* tool1 = (CMYdERIV*)tool

... assuming your class definitions allow it.
 
Share this answer
 
Comments
Member 14594285 8-Nov-23 10:34am    
thanks, but inside a case Can I insert a definition?
Richard MacCutchan 8-Nov-23 10:37am    
Yes, no or maybe. Please update your question, and explain exactly what you are trying to achieve.
Member 14594285 8-Nov-23 10:39am    
Can I write:
case 1:
CMYdERIV* tool1 = (CMYdERIV*)tool;
break;
Richard MacCutchan 8-Nov-23 11:06am    
The compiler will not like that. You need to declare tool1 before the case statement. But if you really want to isolate it from the other cases then You can do it the following way:
	switch (tool->GetMode())
	{
	case 0:
		AfxMessageBox(_T("non contemplato"));
		break;
	case 1:
    {
        CMYdERIV* tool1 = (CMYdERIV*)tool;
        // use tool1 only in this block
    }
        break;

	case 2:
Rick York 8-Nov-23 18:22pm    
It appears we wrote essentially the same thing at the same time and I don't know why you didn't get an up-vote so here you go.
You can do initialization and declaration in a case statement but they must enclosed in braces like this :
C++
case 3:
    {
       CMYderived * tool1 = (CMYderived) tool;
    }
    break;
 
Share this answer
 
Well, you could use a dynamic_cast, try for instance
C++
#include <iostream>
using namespace std;

class A
{
protected:
   int a;
public:
  A(int a):a{a}{}
  virtual void show(){ cout <<  "A " << a << "\n"; }
};

class AB : public A
{
public:
  AB(int a):A{a}{}
  void show() override { cout <<  "AB " << a << "\n"; }
};

class AC : public A
{
public:
  AC(int a):A{a}{}
  void show() override { cout <<  "AC " << a << "\n"; }
};


int main()
{

  A a{10};
  AB ab{20};
  AC ac(30);

  cout << "enter your choice: ";
  int choice;
  cin >> choice;
  switch(choice)
  {
  case 0:
    break;
  case 1:
    {
      auto refAB = dynamic_cast<AB &>(ab); // OK, casting to the correct derived class
      refAB.show();
    }
    break;
  case 2:
    {
      auto refAB = dynamic_cast<AB &>(a); // ERROR, trying to cast an instance of the base class
      refAB.show();
    }
  break;
  case 3:
    {
      auto refAB = dynamic_cast<AB &>(ac); // ERROR, trying to cast an instance of a different derived class
      refAB.show();
    }
  break;
  //...
  default:
  //...
    break;
  }
}


Anyway, such behaviors are usually implemented using polymorphism.
 
Share this answer
 
Comments
Richard MacCutchan 9-Nov-23 3:29am    
+5 for a very comprehensive solution.
CPallini 9-Nov-23 3:45am    
Thank you.
I posted it because the C-style cast, which always succeeds, doesn't look very appropriate there.
Richard MacCutchan 9-Nov-23 4:30am    
That's the problem with C++, you can still use C-code even when it is not the best choice.
CPallini 9-Nov-23 4:37am    
That's ONE of the PROBLEMS with C++ (FFY).
:-D :-D :-D
Richard MacCutchan 9-Nov-23 4:46am    
You mean there are lots more? :(

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900