接口:
template <class T>
class Interface{
public:
typedef T Units;
virtual T get() = 0;
};
Run Code Online (Sandbox Code Playgroud)
Implementation1:
class Implementation1: public Interface<float> {
public:
float get() {
return 0.0f;
}
};
Run Code Online (Sandbox Code Playgroud)
Implementation2:
class Implementation2: public Interface<int> {
public:
int get() {
return 0;
}
};
Run Code Online (Sandbox Code Playgroud)
容器(有错误):
class Container{
private:
Interface* floatGetter;
int n;
Timer::Units* array;
public:
Container(Interface* floatGetter, int n) {
this->floatGetter= floatGetter;
this->n = n;
array = new Timer::Units[n];
}
~Container() {
}
};
Run Code Online (Sandbox Code Playgroud)
有关更多详细信息,我有一个模板接口和一个没有模板的接口的派生类.其他一些类接受派生类的对象,但它将对象作为接口(换句话说,依赖注入).但是这个类中的接口类型是由接口实现定义的.如何在C++中实现这个想法?
EDIT1:
例:
Interface<float> myInterface1 = …Run Code Online (Sandbox Code Playgroud) 如何关闭多个纹理单元,因为它们会影响其他渲染部件.我激活他们:
glActiveTexture(GL_TEXTURE0 + index);
glBindTexture(GL_TEXTURE_2D,
((MaterialSampler2D)specular).texture.getTOB());
shader.setTexture2(index);
Run Code Online (Sandbox Code Playgroud)
有没有类似glDeactivateTexture的东西?
当我将此代码段中的最后一行添加到我的程序代码时:
typedef std::set<Job> JobSet;
typedef boost::shared_ptr<JobSet> JobSetPtr;
JobSetPtr jobs_;
jobs_->insert ( job ); // line 60
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
g++-4.7 -o /home/kron/Software/Synchronizer/1.0/Main.o -c src/Main.cpp
In file included from /usr/include/c++/4.7/string:50:0,
from /usr/include/c++/4.7/bits/locale_classes.h:42,
from /usr/include/c++/4.7/bits/ios_base.h:43,
from /usr/include/c++/4.7/ios:43,
from /usr/include/c++/4.7/ostream:40,
from /usr/include/c++/4.7/iostream:40,
from src/Main.cpp:1:
/usr/include/c++/4.7/bits/stl_function.h: In instantiation of ‘bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = Job]’:
/usr/include/c++/4.7/bits/stl_tree.h:1285:4: required from ‘std::pair<std::_Rb_tree_iterator<_Val>, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(const _Val&) [with _Key = Job; _Val = Job; _KeyOfValue = std::_Identity<Job>; _Compare = std::less<Job>; _Alloc = std::allocator<Job>]’ …Run Code Online (Sandbox Code Playgroud) 我试图在运行时将jar文件添加到classpath.我用这个代码
public static void addURL(URL u) throws IOException {
URLClassLoader sysloader = (URLClassLoader) ClassLoader
.getSystemClassLoader();
Class<URLClassLoader> sysclass = URLClassLoader.class;
try {
Method method = sysclass.getDeclaredMethod("addURL", parameters);
method.setAccessible(true);
method.invoke(sysloader, new Object[] { u });
System.out.println(u);
} catch (Throwable t) {
t.printStackTrace();
throw new IOException("Error");
}
}
Run Code Online (Sandbox Code Playgroud)
系统输出打印此网址:
file:/B:/Java/Tools/mysql-connector-java-5.1.18/mysql-connector-java-5.1.18/mysql-connector-java-5.1.18-bin.jar
Run Code Online (Sandbox Code Playgroud)
我仔细检查了这条路,这个罐子存在.即使这个测试显示com.mysql.jdbc.驱动程序类存在.
javap -classpath "B:\Java\Tools\mysql-connector-java-5.1.18\
mysql-connector-java-5.1.18\mysql-connector-java-5.1.18-bin.jar" com.mysql.jdbc.
Driver
Compiled from "Driver.java"
public class com.mysql.jdbc.Driver extends com.mysql.jdbc.NonRegisteringDriver i
mplements java.sql.Driver{
public com.mysql.jdbc.Driver() throws java.sql.SQLException;
static {};
}
Run Code Online (Sandbox Code Playgroud)
但是当我使用这个Class.forName(驱动程序)时,我仍然得到java.lang.ClassNotFoundException.这段代码有什么问题?
我看了这篇文章.看起来很好.然而,当HotSpot(不依赖于服务器或客户端或不依赖Sun版本)使代码内联时,作者或其他有意识的人会告诉我编码技巧.
我分配一个char数组然后我需要将它作为一个字符串返回,但我不想复制这个char数组然后释放它的内存.
char* value = new char[required];
f(name, required, value, NULL); // fill the array
strResult->assign(value, required);
delete [] value;
Run Code Online (Sandbox Code Playgroud)
我不想像上面那样做.我需要将数组放在std字符串容器中.我怎么能这样做?
EDIT1:
我明白我不应该并且字符串不是为此而设计的.MB有人知道char数组的另一个容器实现,我可以用它吗?
我有一个eclipse rcp应用程序.当这个命令执行时我有一个命令.我需要开始一个线程.执行此线程后,必须更新GUI.但我想这个线程或其他非SWT线程无法更新GUI.但这似乎是合理的.当我试图这样做时,我得到了Exception in thread "Thread-5" org.eclipse.swt.SWTException: Invalid thread access.我如何才能实现这一目标?
Pattern p2 = Pattern.compile(".*");
Matcher m2 = p2.matcher("true");
System.out.println(m2.matches() + " [" + m2.group() + "]");
Run Code Online (Sandbox Code Playgroud)
当我使用上面的代码时,它是可以的.但是当我使用这个regexpr时,我不明白发生了什么[.]*.它打印false.
如何将点作为特定符号?或者如何使用没有\n和\ r \n的任何字符制作一类符号?