在C++中,是否可以使用base和派生类实现单个接口?
例如:
class Interface
{
public:
virtual void BaseFunction() = 0;
virtual void DerivedFunction() = 0;
};
class Base
{
public:
virtual void BaseFunction(){}
};
class Derived : public Base, public Interface
{
public:
void DerivedFunction(){}
};
void main()
{
Derived derived;
}
Run Code Online (Sandbox Code Playgroud)
这会失败,因为Derived无法实例化.就编译器而言,从未定义Interface :: BaseFunction.
到目前为止,我发现的唯一解决方案是在Derived中声明传递函数
class Derived : public Base, public Interface
{
public:
void DerivedFunction(){}
void BaseFunction(){ Base::BaseFunction(); }
};
Run Code Online (Sandbox Code Playgroud)
有没有更好的解决方案?
编辑:如果重要,这是我使用MFC对话框的现实问题.
我有一个从CDialog派生的对话类(MyDialog可以说).由于依赖性问题,我需要创建一个抽象接口(MyDialogInterface).使用MyDialogInterface的类需要使用特定于MyDialog的方法,但还需要调用CDialog :: SetParent.我刚刚通过创建MyDialog :: SetParent并将其传递给CDialog :: SetParent来解决它,但是想知道是否有更好的方法.
我们偶尔需要通知用户有关警告或问题的信息.但通常情况下,特别是如果这是一个常见问题,用户只会忽略警告并继续.通常用户甚至不会记得看到警告,但我们检查他们的日志并看到有几个显示.那么,当你试图告诉他们重要的事情时,你如何让用户注意?
这并不像在允许用户保存之前强制用户解决所有问题一样简单.由于各种原因(通常是针对无法立即解决或根本无法解决的问题),他们经常需要保存我们的业务规则并不严格的数据.
我们有一个更好的警告/错误处理系统,我认为会有很多帮助,但我想看看别人做了什么.
我在Start-Job中使用Start-Process时遇到问题,特别是在使用时-NoNewWindow.例如,这个测试代码:
Start-Job -scriptblock {
Start-Process cmd -NoNewWindow -Wait -ArgumentList '/c', 'echo' | out-null
Start-Process cmd # We'll never get here
}
get-job | wait-job | receive-job
get-job | remove-job
Run Code Online (Sandbox Code Playgroud)
返回以下错误,显然谷歌没有听说过:
Receive-Job:处理来自后台进程的数据时出错.报告错误:无法处理节点类型为"Text"的元素.仅支持Element和EndElement节点类型.
如果我删除-NoNewWindow一切工作就好了.我做的事情愚蠢,还是没有办法开始工作Start-Process -NoNewWindow?有什么好的选择?
我尽可能尽可能地对接口进行编码,但是在集合方面我遇到了一些问题.例如,这里有一些我想要使用的接口.
public interface IThing {}
public interface IThings : IEnumerable<IThing> {}
Run Code Online (Sandbox Code Playgroud)
这是实现.为了实现IEnumerable <IThing>,我需要在Things中显式实现IEnumerable <IThing> .GetEnumerator().
public class Thing : IThing {}
public class Things : List<Thing>, IThings
{
IEnumerator<IThing> IEnumerable<IThing>.GetEnumerator()
{
// This calls itself over and over
return this.Cast<IThing>().GetEnumerator();
}
}
Run Code Online (Sandbox Code Playgroud)
问题是GetEnumerator实现导致堆栈溢出.它一遍又一遍地呼唤着自己.我无法弄清楚为什么它决定调用GetEnumerator的实现而不是this.Cast <IThing>()的结果提供的实现.我有什么想法我做错了吗?我愿意打赌这是非常愚蠢的......
以下是上述类的一些简单测试代码:
static void Enumerate(IThings things)
{
foreach (IThing thing in things)
{
Console.WriteLine("You'll never get here.");
}
}
static void Main()
{
Things things = new Things();
things.Add(new Thing());
Enumerate(things);
}
Run Code Online (Sandbox Code Playgroud) 所以,我有这个非常有用的棒程序:
static void Main(string[] args)
{
new Dictionary<int,int>(10000000);
while (true)
{
System.Threading.Thread.Sleep(1000);
}
}
Run Code Online (Sandbox Code Playgroud)
这甚至不会产生编译器的任何警告,这是令人惊讶的.
运行它会分配一块内存.如果我运行多个副本,我最终会达到一个无法启动的程度,因为我已经没有内存了.
那么这里发生了什么?
我正在尝试使用windbg分析崩溃转储,并且根据加载的符号,我将获得不定的故障转储.我的简单理解是符号只能帮助指向堆栈所指的内容,但堆栈本身是不变的.这显然是错的,但现在我不知道我在看什么.
下面是一个加载了所有符号的调用堆栈:
0:000> kn
# ChildEBP RetAddr
00 0012e120 7d61f60f ntdll!ZwGetContextThread+0x12
01 0012e130 000f0005 ntdll!RtlFreeHeap+0x711
WARNING: Frame IP not in any known module. Following frames may be wrong.
02 0012e1d0 6d5b5b20 0xf0005
03 0012e314 6d5b407f dbghelp!Win32LiveSystemProvider::OpenMapping+0x228
04 0012e464 0012e488 dbghelp!GenAllocateModuleObject+0x1ad
05 0012e4e4 6d5b588e 0x12e488
06 0012e69c 7d4d132f dbghelp!Win32LiveSystemProvider::GetOsCsdString+0x4d
07 0012e6b8 6d5b5fd2 kernel32!ReadProcessMemory+0x1b
08 0012e6e0 6d5b604e dbghelp!Win32LiveSystemProvider::ReadVirtual+0x3d
09 0012e700 6d5b2f3d dbghelp!Win32LiveSystemProvider::ReadAllVirtual+0x1d
0a 0012e728 6d5b304f dbghelp!WriteMemoryFromProcess+0x35
0b 0012e7ac 6d5b345b dbghelp!WriteThreadList+0xc1
0c 0012e7cc 6d5b367b dbghelp!WriteDumpData+0x83
0d 0012e90c 6d5b3778 dbghelp!MiniDumpProvideDump+0x174
*** WARNING: Unable to …Run Code Online (Sandbox Code Playgroud) 我有一个我不拥有的任务(实际上是一个角色,但是在这里使用一个任务使示例变得容易),它对变量执行一些操作。假定变量是整数。我需要以某种方式将其传递给变量,并将其作为int进行传递,但我没有任何运气。
这是我不拥有的任务的超级简化版本:
frob.yml
- name: Validate that frob_count is <= 100
fail: msg="{{frob_count}} is greater than 100"
when: frob_count > 100
- name: Do real work
debug: msg="We frobbed {{frob_count}} times!"
Run Code Online (Sandbox Code Playgroud)
我的剧本是:
- name: Frob some things
hosts: localhost
vars:
things:
- parameter: 1
- parameter: 2
- parameter: 45
tasks:
- with_items: "{{things}}"
include: frob.yml
vars:
frob_count: "{{item.parameter}}"
Run Code Online (Sandbox Code Playgroud)
无论如何,我都会从收到类似“ 1大于100”的错误frob.yml。看起来它把var作为字符串而不是整数。
我尝试过frob_count: "{{item.parameter | int}}"没有运气的东西。如果我可以更改,frob.yml那很容易,但是就像我说的那样,这是我无法控制的。有什么想法吗?
这是在Ansible 2.6.4上
我们有一个应用程序使用简单的单向绑定与GridView来显示一些数据.好吧,现在我们需要允许用户更改一些数据,所以我一直试图让双向数据绑定在GridView中工作.到目前为止,一切都正确显示,但在GridView中编辑单元格似乎什么都不做.我搞砸了什么?像这样的双向数据绑定甚至可能吗?我应该开始转换所有内容以使用不同的控件,比如DataGrid吗?
我写了一个小测试应用程序,显示我的问题.如果您尝试它,您将看到属性设置器在初始化后永远不会被调用.
XAML:
Title="Window1" Height="300" Width="300">
<Grid>
<ListView Name="TestList">
<ListView.View>
<GridView>
<GridViewColumn Header="Strings">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=String, Mode=TwoWay}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Bools">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Path=Bool, Mode=TwoWay}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
这是相应的代码:
using System.Collections.Generic;
using System.Windows;
namespace GridViewTextbox
{
public partial class Window1 : Window
{
private List<TestRow> _rows = new List<TestRow>();
public Window1()
{
InitializeComponent();
_rows.Add(new TestRow("a", false));
_rows.Add(new TestRow("b", true));
_rows.Add(new TestRow("c", false));
TestList.ItemsSource = _rows;
TestList.DataContext = _rows;
}
} …Run Code Online (Sandbox Code Playgroud) c# ×2
c++ ×2
ansible ×1
class ×1
crash-dumps ×1
data-binding ×1
gridview ×1
inheritance ×1
interface ×1
powershell ×1
start-job ×1
usability ×1
warnings ×1
windbg ×1
wpf ×1
xaml ×1