我正在开发一个大量使用模板的库,因此我决定将其设为仅包含 header 的库。由于声明和实现在同一个文件中,我现在可以同时进行。所以我可以在这两种风格之间做出选择:
// seperate declaration and implementation
template <typename T>
class Klass {
public:
void do_something();
};
template <typename T>
void Klass<T>::do_something()
{
// do something
}
// or both at the same time
template <typename T>
class Klass {
public:
void do_something()
{
// do something
}
};
我想知道这两者对于编译器是否有区别。如果不是,您会推荐哪一个作为更好的做法?
最佳答案
如果合并定义和实现,您将无法解决交叉/循环依赖问题。
例如:
home.hpp
#ifndef HOME_HPP
# define HOME_HPP
# include <string>
# include "human.hpp"
template <typename T>
class Home
{
public:
Home()
:
color_("white"),
inhabitant_(this)
{
}
void set_color(const std::string& color)
{
color_ = color;
}
private:
std::string color_;
Human<T> inhabitant_;
};
#endif
human.hpp
#ifndef HUMAN_HPP
# define HUMAN_HPP
# include <string>
// Cannot include the other header here because it already includes us
//# include "house.hpp"
// So we do a forward declaration:
template<typename T>
class Home;
template <typename T>
class Human
{
public:
Human(Home<T>* house)
:
house_(house)
{
}
void paint(const std::string& color)
{
// Oops, we can't use House in our implementation
// because it is only forward declared
//house_->set_color(color);
}
private:
Home<T>* house_;
};
#endif
您不能使用其他对象。
而如果您将实现拆分到另一个文件中:
home.hpp
#ifndef HOME_HPP
# define HOME_HPP
# include <string>
# include "human.hpp"
template <typename T>
class Home
{
public:
Home();
void set_color(const std::string& color);
private:
std::string color_;
Human<T> inhabitant_;
};
#endif
home.ipp
#ifndef HOME_IPP
# define HOME_IPP
# include "home.hpp"
template<typename T>
Home<T>::Home()
:
color_("white"),
inhabitant_(this)
{
}
template<typename T>
void Home<T>::set_color(const std::string& color);
{
color_ = color;
}
#endif
human.hpp
#ifndef HUMAN_HPP
# define HUMAN_HPP
# include <string>
template<typename T>
class Home;
template <typename T>
class Human
{
public:
Human(Home<T>* house);
void paint(const std::string& color);
private:
Home<T>* house_;
};
#endif
human.ipp
#ifndef HUMAN_IPP
# define HUMAN_IPP
# include "human.hpp"
# include "house.ipp"
template<typename T>
Human<T>::Human(Home<T>* house)
:
house_(house)
{
}
template<typename T>
void Human<T>::paint(const std::string& color)
{
house_->set_color(color);
}
#endif
事情更顺利
关于c++ - 在仅 header 库的类定义中实现有什么不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25890133/
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
我正在尝试设置一个puppet节点,但rubygems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由rubygems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby
我怎样才能完成http://php.net/manual/en/function.call-user-func-array.php在ruby中?所以我可以这样做:classAppdeffoo(a,b)putsa+benddefbarargs=[1,2]App.send(:foo,args)#doesn'tworkApp.send(:foo,args[0],args[1])#doeswork,butdoesnotscaleendend 最佳答案 尝试分解数组App.send(:foo,*args)
我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢