简体   繁体   English

我可以通过模板c ++将对象传递给类吗

[英]can I pass an object to a class via template c++

Can I do this: 我可以这样做吗?

class A
{
    public:
    void print()
    {
         std::cout<<"A"<<std::endl;
    }
};

class B
{
    public:
    void print()
    {
         std::cout<<"B"<<std::endl;
    }
};

template <class T>
class C 
{
     public:
     void say()
     {
          T.print();
     }
};

int main()
{
     A a;
     B b;
     C<a> c;
     c.say();
     C<b> d;
     d.say();

     return 0;
 }

If I don't want to use a base class for A and B , what is the best way (fast running speed) way of doing this? 如果我不想对AB使用基类,那么最好的方法(快速运行)是什么?

There are multiple things wrong with your code. 您的代码有很多错误。

You seem to be mixing up types with instances (or classes and objects). 您似乎将类型与实例(或类和对象)混合在一起。

This code should actually compile and work as intended. 该代码实际上应按预期进行编译和工作。

#include <iostream>

class A
{
    public:
    void print()
    {
         std::cout<<"A"<<std::endl;
     }
};

class B
{
    public:
    void print()
    {
         std::cout<<"B"<<std::endl;
     }
};

template <class T>
class C 
{
     public:
     void say(T obj)
     {
          obj.print();
          // If T::print() were static, you could call it like that instead.
     }
};

int main()
{
     A a;
     B b;
     C<A> c; // template for C demands a class, not an object!
             // in practice this would mean you write 'A' instead of 'a' here
     c.say(a);
     C<B> d; // same here
     d.say(b);
}

Where T is a type T.print(); 其中T是类型T.print(); is not valid C++. 无效的C ++。 Instead you have the following options: 相反,您有以下选择:

If you make print a static method you could do: 如果将print设为静态方法,则可以执行以下操作:

void say()
{
     T::print();
}

This might be a good route if the print method does not need any state. 如果print方法不需要任何状态,这可能是一个不错的选择。 It's nothing more that than a straight (possibly inlined) function call. 只是一个直接的(可能是内联的)函数调用而已。 So not much scope for performance problems. 因此,性能问题的范围不大。

Otherwise you have to pass the object : 否则,您必须传递对象

void say(T object)
{
    object.print();
}

There are some options for how you would pass the object. 对于如何传递对象,有一些选择。 Whether by value as above (assuming T isn't a reference/pointer type) or by reference. 无论是通过上述值(假设T不是引用/指针类型)还是通过引用。 These may or may not have significance on the performance depending on the particular case. 根据具体情况,这些对性能可能有影响,也可能没有影响。

Or create an instance within the say method: 或在say方法内创建一个实例:

void say()
{
    T().print();
}

For simple cases this is likely to optimise out to be essentially the same as the static print method case. 对于简单的情况,这很可能会优化为与静态print方法的情况基本相同。 Though of course the constructing a T could be potentially costly for less trivial cases. 尽管对于不那么琐碎的情况,构建T可能当然会花费很大。

At first you forgot the semicolons behind the class declarations and somehow your main does not return an integer. 最初,您忘记了类声明后面的分号,并且您的main不以某种方式返回整数。 So assuming thats fixed there is only a small amount of code there, which is still wrong, but is a huge impact. 因此,假设那是固定的,那么那里只有少量的代码,这仍然是错误的,但是影响很大。 You mixed up run time and compile time. 您混合了运行时间和编译时间。

 A a;
 B b;
 C<a> c;
 C<b> d;

You try to use a non-const instance as a template parameter, which makes no sense. 您尝试将非常量实例用作模板参数,这没有任何意义。 It will be created on runtime, but on compiletime you have no clue about those. 它将在运行时创建,但是在编译时您对此一无所知。 A template parameter does not take a non-const value, most of time it simply takes a type , like in your code. 模板参数通常不采用非const值,大多数情况下仅采用type ,就像在代码中那样。 template<class T> or template<typename T> is expecting some type, not an instance of something. template<class T>template<typename T>期望某种类型,而不是某种实例。 However you can recieve instances by doing things like template<A myConstA> . 但是,您可以通过执行诸如template<A myConstA>类的操作来接收实例。 Anyways you need to rewrite it, such that it recieves the type , as it is known at compile-time. 无论如何,您都需要重写它,以使其接受type ,这在编译时是已知的。

A a;
B b;

//you can do C<decltype(a)>, too
C<A> c;
C<B> d;

You have your print function like this: 您具有如下打印功能:

void print()
{ T.print(); }

But you cannot call a non-static function without an instance! 但是,如果没有实例,就无法调用非静态函数! Thus you have a few possibilities to fix this. 因此,您有几种方法可以解决此问题。 Either take a reference of type T and call from that, like so: 要么引用类型T的引用,然后从中调用,就像这样:

void print(T& t)
{ t.print(); }

or simply, if ok, do this: 或者简单地,如果可以,请执行以下操作:

void print()
{ T().print(); }

or make the print functions of both A and B static! 或将ABprint功能都B静态! Try it online ! 在线尝试!


By the way whatever you do, if you decide not to make it static, make it a const function and noexcept , since it never throws and does change nothing from the instance. 顺便说一句,无论您做什么,如果决定不使其静态化,请使其成为const函数和noexcept ,因为它从不抛出并且不会对实例进行任何更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过在C ++中创建对象将参数传递给类? - How can I pass arguments to a class by creating an object in C++? C ++:如何通过第三个对象将数据从一个对象传递到另一个对象的数组? - C++: How can I pass data from an object to an array of another object via a third object? 如何将数组对象(Class Template c ++)作为参数传递给函数? - How do I Pass an array object (Class Template c++) to a function as a parameter? 我可以使用c ++中的模板类来完成此操作 - Can I do this with a template class in c++ 如何在不在C ++中创建函数对象的情况下将函数指针作为模板值参数传递? - How can I pass a function pointer as a template value argument without creating a function object in C++? 在C ++中声明该类的对象时,我可以将模板类'指针作为参数吗? - Can I have the template class' pointer as a parameter when declaring an object of that class' type in C++? 我可以传递从模板类继承的对象类吗? - can i pass object class that is inherited from template class? 通过模板参数传递数据,C++ - Pass data via template argument, C++ C ++如何使用模板类调用模板函数? - C++ How can I call a template function with a template class? 我可以从模板类中提取C ++模板参数吗? - Can I extract C++ template arguments out of a template class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM