这两个结构是否具有相同的内存布局?(C++)
struct A
{
int x;
char y;
double z;
};
struct B
{
A a;
};
Run Code Online (Sandbox Code Playgroud)
如果我手动将此对象转换为A?,我还可以访问x,y,z成员吗?
struct C
{
A a;
int b;
};
Run Code Online (Sandbox Code Playgroud)
提前致谢.
编辑:
如果他们而classes不是structs?
我只是想做一件简单的事情.我在目录中获得了以下文件:
AppInterface.h baa PEMsg.h PluginInterface.h
Run Code Online (Sandbox Code Playgroud)
然后我发出命令:
ls | grep -v ".h" | rm -rf
Run Code Online (Sandbox Code Playgroud)
令我沮丧的是,baa不会被删除.但是这个:
ls | grep -v ".h"
Run Code Online (Sandbox Code Playgroud)
baa按照我的预期给出.所以我猜问题是如何rm输入,但我不知道为什么.在csh和bash中试过这个.
我今天一直在写循环.所有这些都具有以下格式:
for(size_t szI = X; szI < Y; ++szI)
{
//Something
}
Run Code Online (Sandbox Code Playgroud)
而且我知道有办法在vim中记录动作.如果X和Y可以改变,一旦我以某种方式提供X和Y,我可以在vim中执行某些操作来编写for循环吗?
我需要在ui中显示一个对象的细节.我有以下代码,它定义了类的数据模板Category.我试图获得一个布局,其中成员名称和值的对将垂直堆叠.但是,我希望成员名称的宽度相同,值相同.我该怎么做才能获得这种布局?
<DataTemplate DataType="{x:Type local:Category}">
<Grid HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="4*" SharedSizeGroup="a" />
<ColumnDefinition Width="6*" SharedSizeGroup="b" />
</Grid.ColumnDefinitions>
<StackPanel>
<StackPanel HorizontalAlignment="Stretch" Orientation="Horizontal" Grid.ColumnSpan="2">
<TextBlock Text="Id" Grid.Column="0" Grid.IsSharedSizeScope="True" />
<TextBlock Text="{Binding Path=Id}" Grid.Column="1"/>
</StackPanel>
<StackPanel HorizontalAlignment="Stretch" Orientation="Horizontal" Grid.ColumnSpan="2">
<TextBlock Text="Name" Grid.Column="0" Grid.IsSharedSizeScope="True" />
<TextBlock Text="{Binding Path=Name}" Grid.Column="1"/>
</StackPanel>
</StackPanel>
</Grid>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud) 我有以下结构将用于保存插件信息.我很确定这会随着时间的推移而改变(最可能添加).假设这个文件将被修复,这里有什么比我做的更好吗?
struct PluginInfo
{
public:
std::string s_Author;
std::string s_Process;
std::string s_ReleaseDate;
//And so on...
struct PluginVersion
{
public:
std::string s_MajorVersion;
std::string s_MinorVersion;
//And so on...
};
PluginVersion o_Version;
//For things we aren't prepared for yet.
void* p_Future;
};
Run Code Online (Sandbox Code Playgroud)
此外,在为此系统构建共享对象时,是否应采取任何预防措施.我的预感是我会遇到很多库兼容性问题.请帮忙.谢谢
我一直在研究这个插件系统.我以为我通过设计并开始实施.现在我想知道我是否应该重温我的设计.我的问题如下:
目前在我的设计中我有:
一个接口类,FileNameLoader用于加载我的应用程序需要加载的所有共享库的名称.即加载目录中的所有文件,加载XML文件中指定的所有文件,加载所有文件用户输入等.
LibLoader实际加载共享对象的Interface类.该类仅负责在给定文件名后加载共享对象.加载共享库可能需要多种方法.即使用RTLD_NOW/ RTLD_LAZY....,检查是否已经加载了lib等.
一个ABC Plugin,一旦提供了句柄,就从句柄到库加载我需要的函数.有很多方法可以改变.
PluginFactory创建Plugins 的接口类.
ABC PluginLoader是管理一切的母班.
现在,我的问题是我觉得FileNameLoader和LibLoader能进去Plugin.但这意味着,如果有人想改变RTLD_NOW,RTLD_LAZY他将不得不改变Plugin阶级.另一方面,我觉得这里的课程太多了.请提供一些意见.如有必要,我可以发布接口代码.提前致谢.
编辑:
在给出了一些想法之后,我得出的结论是更多接口更好(至少在我的场景中).假设有x的实现FileNameLoader,y的实现LibLoader,z的实现Plugin.如果我将这些类分开,我必须编写x + y + z实现类.然后我可以将它们组合起来以获得任何功能.另一方面,如果所有这些接口都在Plugin类中,我必须编写x*y*z实现类来获得所有可能的功能,这些功能大于x + y + z鉴于接口至少有2个实现.这只是它的一个方面.另一个优点是,当有更多接口时,接口的目的更加清晰.至少这是我的想法.
#include <iostream>
#include <map>
using namespace std;
int main()
{
int x = 5;
decltype(x) y = 10;
map<int, int> m;
decltype(m) n;
decltype(m)::iterator it;
}
g++ -std=c++0x main.cpp
main.cpp: In function `int main()':
main.cpp:11: error: expected initializer before `it'
Run Code Online (Sandbox Code Playgroud)
前两个decltypes工作.第三个导致编译器错误.这是这个gcc版本的问题吗?
g++ -v
Using built-in specs.
Target: x86_64-suse-linux
Configured with: ../configure --prefix=/usr --infodir=/usr/share/info --mandir=/usr/share/man --libdir=/usr/lib64 --libexecdir=/usr/lib64 --enable-languages=c,c++,objc,fortran,obj-c++,java,ada --enable-checking=release --with-gxx-include-dir=/usr/include/c++/4.3 --enable-ssp --disable-libssp --with-bugurl=http://bugs.opensuse.org/ --with-pkgversion='SUSE Linux' --disable-libgcj --disable-libmudflap --with-slibdir=/lib64 --with-system-zlib --enable-__cxa_atexit --enable-libstdcxx-allocator=new --disable-libstdcxx-pch --enable-version-specific-runtime-libs --program-suffix=-4.3 --enable-linux-futex --without-system-libunwind --with-cpu=generic --build=x86_64-suse-linux
Thread model: posix …Run Code Online (Sandbox Code Playgroud) 这是静态断言技巧的一部分.我无法理解非专业化的类是如何工作的.有人可以向我解释一下吗?
编辑:宏代码:(摘自http://www.skynet.ie/~caolan/Fragments/C++StaticAssert.html)
#ifndef STATICASSERT_HXX
#define STATICASSERT_HXX
/*
Lifted direct from:
Modern C++ Design: Generic Programming and Design Patterns Applied
Section 2.1
by Andrei Alexandrescu
*/
namespace ww
{
template<bool> class compile_time_check
{
public:
compile_time_check(...) {}
};
template<> class compile_time_check<false>
{
};
}
/*
Similiar to assert, StaticAssert is only in operation when NDEBUG is not
defined. It will test its first argument at compile time and on failure
report the error message of the second argument, which must …Run Code Online (Sandbox Code Playgroud) 事实证明,这个条件块在我的代码中不断重复.还有其他方法让我的生活更轻松吗?当然,对于一个条件要执行的身体是不同的.
if self.datatype == "string":
t = "z"
elif self.datatype == "double":
t = "d"
elif self.datatype == "number":
t = "i"
elif self.datatype == "blob":
t = "z"
else:
raise EntParEx("Unknown datatype" + self.datatype)
Run Code Online (Sandbox Code Playgroud)
......更多代码使用相同的条件
def emit_cpp_def(self):
s = ""
e = ""
if self.datatype == "string":
s += "static const int " + self.lenvar + " = " + self.length + ";"
s += "\nchar"
e += "[" + self.lenvar + " + 2" + "]"
elif self.datatype …Run Code Online (Sandbox Code Playgroud) 我有以下代码。我试图将Command的属性绑定到在用户控件MenuItem中实现的相关命令。DataContext谁能帮我?该命令不会被执行。
<UserControl x:Class="MIB2.App.Presentations.Views.CategoryManView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:model="clr-namespace:MIB2.Models;assembly=MIB2"
xmlns:local="clr-namespace:MIB2.App.Presentations.Views"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="335">
<UserControl.Resources>
<local:DatabindingDebugConverter x:Key="debugConverter" />
</UserControl.Resources>
<StackPanel Width="417">
<TreeView x:Name="tree"
ItemsSource="{Binding RootCategories}" SelectedItemChanged="TreeView_SelectedItemChanged"
MouseRightButtonDown="OnPreviewMouseRightButtonDown">
<TreeView.ContextMenu>
<ContextMenu>
<MenuItem Header="Add Root Category" Command="{Binding AddRootCategoryCommand}"/>
</ContextMenu>
</TreeView.ContextMenu>
<TreeView.ItemTemplate>
<HierarchicalDataTemplate DataType="model:Item"
ItemsSource="{Binding Children}">
<Grid>
<Grid.ContextMenu>
<ContextMenu DataContext="{Binding PlacementTarget.Tag, RelativeSource={RelativeSource AncestorType=TextBox}}">
<MenuItem Header="Add Category" Command="{Binding Path=AddEntityCommand}" />
</ContextMenu>
</Grid.ContextMenu>
<TextBlock Text="{Binding Description}" Margin="0,0,10,0" Tag="{Binding DataContext, ElementName=tree}"/>
</Grid>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
<StackPanel Orientation="Horizontal">
<Button Content="Add New Category" Command="{Binding AddEntityCommand}" …Run Code Online (Sandbox Code Playgroud)