C++ - 将模板类的指针传递给函数

发布于 2025-01-11 08:39:59 字数 758 浏览 0 评论 0 原文

我正在尝试将指向模板对象的指针传递给另一个类。

template <int size>
class A {
public:
    int a[size] = {0};

    int getA(int n) {
       return a[n];
    }
};


class B {
public:
    A<>* b;

    void setB(A<>* n) {
        b = n;
    }
};


int main()
{
    const int size1 = 10;

    A<size1> data1;
    B b1;
    
    b1.setB(&data1);
}

这是行不通的。

作为解决方案,我可以创建 B 类作为模板类,并将 B 对象创建为 B> b1; 但是,如果我乘以 A,这将创建多个对象,这是我不想要的,因为此代码适用于资源有限的嵌入式项目。

我想要的只是将 data1 对象的指针传递给另一个类函数并将其存储在其中。我正在寻找的代码适用于 C++03,我无法使用 C++11 功能,例如共享指针。

有办法做到这一点吗?

感谢任何帮助,

I am trying to pass a pointer to a templated object to another class.

template <int size>
class A {
public:
    int a[size] = {0};

    int getA(int n) {
       return a[n];
    }
};


class B {
public:
    A<>* b;

    void setB(A<>* n) {
        b = n;
    }
};


int main()
{
    const int size1 = 10;

    A<size1> data1;
    B b1;
    
    b1.setB(&data1);
}

Which doesn't work.

As a solution, I can create the B class as a template class and create B object as B<A<size1>> b1; but this will create multiple objects if I multiply A<sizeX>, which I don't want since this code is for an embedded project which has finite resources.

All I want is to pass the pointer of data1 object to another class function and store it inside. The code I'm looking for is for C++03, I cannot use C++11 features such as shared pointers.

Is there a way to do this?

Appreciate any help,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柳絮泡泡 2025-01-18 08:39:59

你已经让自己陷入了某种“第 22 条军规”的境地。

如果不使 B 模板化,您就无法在 B 中保留模板化 A ,例如

template <int size>
class A {
public:
    int a[size];

    int getA(int n) {
       return a[n];
    }
};

template <int size>
class B {
public:
    A<size>* b;

    int getA(int n) {
        return b->getA(n);
    }

    void setB(A<size>* n) {
        b = n;
    }
};

int main()
{
    const int size1 = 10;

    A<size1> data1;
    B<size1> b1;
    
    b1.setB(&data1);
    int a = b1.getA(0);
}

:给A一个非模板化基类供B保存,例如:

class A_base
{
    virtual ~A_base() {}
    virtual int getA(int n) = 0;
};

template <int size>
class A : public A_base {
public:
    int a[size];

    int getA(int n) {
       return a[n];
    }
};

class B {
public:
    A_base* b;

    int getA(int n) {
       return b->getA(n);
    }

    void setB(A_base* n) {
        b = n;
    }
};

int main()
{
    const int size1 = 10;

    A<size1> data1;
    B b1;
    
    b1.setB(&data1);
    int a = b1.getA(0);
}

否则,你将不得不制作B 只需持有一个void*指针,然后要求调用者提取void* 并决定将其转换为什么,例如:

template <int size>
class A {
public:
    int a[size];

    int getA(int n) {
       return a[n];
    }
};

class B {
public:
    void* b;

    void* getB() {
        return b;
    }

    void setB(void* n) {
        b = n;
    }
};

int main()
{
    const int size1 = 10;

    A<size1> data1;
    B b1;
    
    b1.setB(&data1);
    int a = static_cast< A<size1>* >(b1.getB())->getA(0);
}

You have gotten yourself into a bit of a catch-22 situation.

You can't hold a templated A inside of B without making B templated as well, eg:

template <int size>
class A {
public:
    int a[size];

    int getA(int n) {
       return a[n];
    }
};

template <int size>
class B {
public:
    A<size>* b;

    int getA(int n) {
        return b->getA(n);
    }

    void setB(A<size>* n) {
        b = n;
    }
};

int main()
{
    const int size1 = 10;

    A<size1> data1;
    B<size1> b1;
    
    b1.setB(&data1);
    int a = b1.getA(0);
}

Or by giving A a non-templated base class for B to hold, eg:

class A_base
{
    virtual ~A_base() {}
    virtual int getA(int n) = 0;
};

template <int size>
class A : public A_base {
public:
    int a[size];

    int getA(int n) {
       return a[n];
    }
};

class B {
public:
    A_base* b;

    int getA(int n) {
       return b->getA(n);
    }

    void setB(A_base* n) {
        b = n;
    }
};

int main()
{
    const int size1 = 10;

    A<size1> data1;
    B b1;
    
    b1.setB(&data1);
    int a = b1.getA(0);
}

Otherwise, you will have to make B just hold a void* pointer, and then require the caller to extract that void* and decide what to cast it to, eg:

template <int size>
class A {
public:
    int a[size];

    int getA(int n) {
       return a[n];
    }
};

class B {
public:
    void* b;

    void* getB() {
        return b;
    }

    void setB(void* n) {
        b = n;
    }
};

int main()
{
    const int size1 = 10;

    A<size1> data1;
    B b1;
    
    b1.setB(&data1);
    int a = static_cast< A<size1>* >(b1.getB())->getA(0);
}
谈下烟灰 2025-01-18 08:39:59

我认为您需要从构造函数传递大小。模板不适合此用例。

I think you will want to pass the size from a constructor. Templates don't fit for this use case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文