在交换机中声明类对象,然后在交换机外部使用该变量

时间:2022-08-21 22:51:48

Is there a way to work around this?Im declaring class objects in a switch statement and later using that variable outside the switch, it only works if i put the rest of my code in each case which isnt very efficient.Heres my code

有没有办法解决这个问题?我在switch语句中声明类对象,然后在交换机外部使用该变量,只有在我的代码中放入其余的代码并不是非常有效时,它才有效。这是我的代码

switch (shape)
{
case 'q':
{
    Quad geo(a,b,c,d);
}
break;
case 'r':
{
    Rectangle geo(a,b,c,d);
}
break;
case 't':
{
    Trapezoid geo(a,b,c,d);
}
break;
case 'p':
{
    Parrelogram geo(a,b,c,d);
}
break;
case 's':
{
    Square geo(a,b,c,d);

}
break;
default:
    break;
}

 geo.print();//obviously wont work

2 个解决方案

#1


3  

Have an IPrintable interface, like this

有一个IPrintable接口,像这样

struct IPrintable
{
    virtual ~IPrintable() {}
    virtual void Print() = 0;
};

Then, derive your types Quad, Rectangle, etc from IPrintable, i.e. implement that interface. Then your code looks like this:

然后,从IPrintable派生您的类型Quad,Rectangle等,即实现该接口。然后你的代码看起来像这样:

std::unique_ptr<IPrintable> pShape;
switch(shape)
{
    case quad:
       pShape.reset(new Quad(...));
    case rect
       pShape.reset(new Rect(...));
}
if(pShape)
    pShape->Print();

Of course, if the common functionality is more than print, you can add those functions to the interface as well. Also take a look at the visitor pattern. It may or may not be of help to you depending on the specifics of your problem.

当然,如果常用功能不仅仅是打印,您也可以将这些功能添加到界面中。另请查看访客模式。根据您问题的具体情况,它可能对您有所帮助,也可能没有帮助。

#2


1  

No, it is not possible. geo can only have one type at compile time, and it cannot change at runtime.

不,这是不可能的。 geo在编译时只能有一种类型,并且在运行时不能更改。

You could do something similar with dynamic allocation and polymorphism, but it might not be the best solution to your problem.

您可以使用动态分配和多态来执行类似操作,但它可能不是解决您问题的最佳方法。

With the knowledge that Quad is the base class of the others, the following might be a usable solution:

由于Quad是其他人的基类,以下可能是一个有用的解决方案:

Quad* geo = 0;
switch (shape) {
case 'q':
    geo = new Quad(a,b,c,d);
    break;
case 'r':
    geo = new Rectangle(a,b,c,d);
...
default:
    break;
}
if (geo) geo->print();
delete geo; // Ok if geo is 0.

This solution is not particularly pretty, mainly because it uses raw pointers and new and delete directly. A more polished version would use the Factorypattern, returning a smart pointer.

这个解决方案并不是特别漂亮,主要是因为它直接使用原始指针和new和delete。更精致的版本将使用Factorypattern,返回智能指针。

#1


3  

Have an IPrintable interface, like this

有一个IPrintable接口,像这样

struct IPrintable
{
    virtual ~IPrintable() {}
    virtual void Print() = 0;
};

Then, derive your types Quad, Rectangle, etc from IPrintable, i.e. implement that interface. Then your code looks like this:

然后,从IPrintable派生您的类型Quad,Rectangle等,即实现该接口。然后你的代码看起来像这样:

std::unique_ptr<IPrintable> pShape;
switch(shape)
{
    case quad:
       pShape.reset(new Quad(...));
    case rect
       pShape.reset(new Rect(...));
}
if(pShape)
    pShape->Print();

Of course, if the common functionality is more than print, you can add those functions to the interface as well. Also take a look at the visitor pattern. It may or may not be of help to you depending on the specifics of your problem.

当然,如果常用功能不仅仅是打印,您也可以将这些功能添加到界面中。另请查看访客模式。根据您问题的具体情况,它可能对您有所帮助,也可能没有帮助。

#2


1  

No, it is not possible. geo can only have one type at compile time, and it cannot change at runtime.

不,这是不可能的。 geo在编译时只能有一种类型,并且在运行时不能更改。

You could do something similar with dynamic allocation and polymorphism, but it might not be the best solution to your problem.

您可以使用动态分配和多态来执行类似操作,但它可能不是解决您问题的最佳方法。

With the knowledge that Quad is the base class of the others, the following might be a usable solution:

由于Quad是其他人的基类,以下可能是一个有用的解决方案:

Quad* geo = 0;
switch (shape) {
case 'q':
    geo = new Quad(a,b,c,d);
    break;
case 'r':
    geo = new Rectangle(a,b,c,d);
...
default:
    break;
}
if (geo) geo->print();
delete geo; // Ok if geo is 0.

This solution is not particularly pretty, mainly because it uses raw pointers and new and delete directly. A more polished version would use the Factorypattern, returning a smart pointer.

这个解决方案并不是特别漂亮,主要是因为它直接使用原始指针和new和delete。更精致的版本将使用Factorypattern,返回智能指针。