小编Isa*_*aan的帖子

Polymorphism with different parameters

By any chance is it possible to do polymorphism with the same name of function with different parameters?

For example, I want those three functions to become one.

virtual bool isValid1(const std::string&) = 0;
virtual bool isValid2(const uint32_t&) = 0;
virtual bool isValid3(const int& num) = 0;
Run Code Online (Sandbox Code Playgroud)

换句话说,我有一个Base A和三个Derived A类。我也有基本B保持基质A类和T的矢量(模板类型,可以是一个stringunit32int)参数。当我想从基地调用BisValid使用功能vector和参数(这样的:isValid(argument))我有点卡住了,因为我知道,如果前向呼叫isValid1(string)isValid2(uint32)isValid3(int)

对于那些询问的人,有一些代码,

template <class T>
class Field
{ …
Run Code Online (Sandbox Code Playgroud)

c++

5
推荐指数
1
解决办法
132
查看次数

源自两个基础 - 删除矢量**奇怪的**问题

我正在努力想象出问题出在哪里,但看起来很奇怪.

我以更容易理解的方式重写了我的问题.

当它到达删除行时,debbug会产生一个断点.

PS.有趣的是,如果我们采用int b1并将其移动到Base2就行了.

BASE1:

#pragma once
class Base1
{
public:
    Base1();
    ~Base1();
    int b1;
};
Run Code Online (Sandbox Code Playgroud)

Base2.h:

#pragma once
#include <iostream>
#include <vector>
class Derived;
class Base2
{
public:
    Base2();
    ~Base2();
    std::vector <std::vector <Base2*>> vec;
    void doSomething(Derived& d);
};
Run Code Online (Sandbox Code Playgroud)

Base2.cpp:

#include "Base2.h"
#include "Derived.h"


Base2::Base2()
{
    //the numbers 1 and 5 does not really metter
    vec.resize(1);
    vec[0].resize(5);
}


Base2::~Base2()
{
}

void Base2::doSomething(Derived& d)
{
    vec[0][d.b1] = new Derived();
    delete vec[0][d.b1];
}
Run Code Online (Sandbox Code Playgroud)

衍生:

#pragma once
#include "Base1.h"
#include "Base2.h" …
Run Code Online (Sandbox Code Playgroud)

c++

4
推荐指数
1
解决办法
67
查看次数

标签 统计

c++ ×2