我想在向量中存储几个类的实例.由于所有类都继承自相同的基类,因此这是可能的.
想象一下这个程序:
#include <iostream>
#include <vector>
using namespace std;
class Base
{
public:
virtual void identify ()
{
cout << "BASE" << endl;
}
};
class Derived: public Base
{
public:
virtual void identify ()
{
cout << "DERIVED" << endl;
}
};
int main ()
{
Derived derived;
vector<Base> vect;
vect.push_back(derived);
vect[0].identify();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我希望它能打印"DERIVED",因为"识别"方法是虚拟的.相反,'vect [0]'似乎是一个'Base'实例,它打印出来
基础
我想我可以写一个我自己的容器(可能是从vector派生的),不知何故能够做到这一点(也许只能拿指针......).我只是想问一下是否有更多的C++ ish方法来做到这一点.我希望完全兼容矢量(只是为了方便其他用户应该使用我的代码).
我知道这个问题可能听起来很奇怪,因为GLib是一个可移植性库,但它有多便携?举一个例子:GLib(包括GObject)是在微控制器上运行还是依赖于某种操作系统?
我可以在哪里使用GLib有限制,还是可以在任何可以运行C代码的地方运行?
我知道有很多关于OpenGL的教程,但是我发现所有好的教程都使用了一些C++库(大多数是GLM),这些库很难让那些想要使用C或/和特定于Microsoft Windows的人使用.
我的问题是:
我想创建一个服务器和客户端应用程序,使用GIO通过套接字进行通信.GSocketService和GSocketClient看起来很适合这个目的,但不幸的是我找不到一些教程或示例代码(GLib,GIO,...新手可以理解).有人知道一些好的资源或者可以在这里发布示例代码吗?
我正在寻找一种方法将抽象语法树"转储"到文件中,以便可以使用编译器解析代码,然后以独立于语言和编译器的方式存储.然而,我无法找到任何被广泛认可的方式来做到这一点.这样的方式存在吗?
我正在尝试使用GtkBuilder和glade 创建一个非常简单的GUI.为了实现这一点,我遵循官方的Gtk + 3参考手册的教程.与原始代码的唯一区别是,为了简单起见,我没有连接到小部件的信号(因此也删除了它们的回调函数):
#include <gtk/gtk.h>
int
main (int argc,
char *argv[])
{
GtkBuilder *builder;
GObject *window;
GObject *button;
gtk_init (&argc, &argv);
/* Construct a GtkBuilder instance and load our UI description */
builder = gtk_builder_new ();
gtk_builder_add_from_file (builder, "builder.ui", NULL);
gtk_main ();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
本教程中使用的"builder.ui"文件如下所示:
<interface>
<object id="window" class="GtkWindow">
<property name="visible">True</property>
<property name="title">Grid</property>
<property name="border-width">10</property>
<child>
<object id="grid" class="GtkGrid">
<property name="visible">True</property>
<child>
<object id="button1" class="GtkButton">
<property name="visible">True</property>
<property name="label">Button 1</property>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">0</property> …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用以下代码在C++中创建链接列表
int main ()
{
return 0;
}
class LList
{
private:
struct node
{
int value;
node *follower; // node definitely is a node
};
node m_list;
int m_length;
public:
LList ();
void insert (int index, int value);
int get_length () const { return m_length; }
};
LList::LList ()
{
m_length = 0;
m_list.follower = 0;
}
void LList::insert (int index, int value)
{
node *cur_node = &m_list;
for (int count=0; count<index; count++)
{
cur_node = cur_node.follower; // << …Run Code Online (Sandbox Code Playgroud) c ×2
c++ ×2
client ×1
compilation ×1
gio ×1
glade ×1
glib ×1
gtk ×1
linked-list ×1
opengl ×1
polymorphism ×1
portability ×1
sockets ×1