我目前有一个派生类和一个基类.如何使派生类的基类等于我拥有的基类?浅拷贝会起作用吗?
class Base
{
private string name;
public string Name { get; set; }
private string address;
public string Address { get; set; }
}
class Derived:Base
{
private string field;
public String field { get; set; }
}
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Base b = new Base();
b.Address = "Iliff";
b.Name = "somename";
Derived d = new Derived();
//How can I make the base class of d equal to b ?
} …Run Code Online (Sandbox Code Playgroud) 假设我有两个shared_ptr类型,例如
boost::shared_ptr<ObjA> sptrA;
boost::shared_ptr<ObjB> sptrB;
Run Code Online (Sandbox Code Playgroud)
现在假设sptrA->SomeMethod()返回了一个简单的ObjB类型(不是共享ptr).我可以在sptrB中以某种方式存储该类型吗?这样我就可以做这样的事情,以便返回的类型实例自动转换为boost_shared ptr
sptrB = sptrA->SomeMethod();
Run Code Online (Sandbox Code Playgroud)
我只是好奇地询问这个问题,是否可能?
在我的代码中,我有一个接口 - 让我们说它的被调用InterfaceName和它的实现调用InterfaceImpl.现在,当我动态尝试InterfaceImpl使用以下代码获取时:
object obj = Activator.CreateInstance("ProjectName","ProjectName.Folder.InterfaceImpl");
InterfaceName in = (InterfaceName)obj; //Error pops up here
Run Code Online (Sandbox Code Playgroud)
我收到以下错误
Unable to cast object of type 'System.Runtime.Remoting.ObjectHandle' to type 'ProjectName.Folder.InterfaceName'.
Run Code Online (Sandbox Code Playgroud)
什么可能出错?
我在一个dll中有一个函数,我想从我的C++应用程序调用.dll也是用C++编写的,有一个def文件,显示了dll中的函数.我正在使用visual studio 2010,我将它配置为使用dll文件,方法是在链接器"Additional Library directories"中添加dll的目录,然后在链接器的"input"中添加DLLname.lib.现在dll中的所有命名空间都可用,但是我需要的函数不可用,因为它们不在任何命名空间下.我该如何访问这些功能?这些函数在dll中声明为这样
#include "stdafx.h"
#include <stdio.h>
__declspec(dllexport) int somefunction()
{
......
return SomeValue
}
Run Code Online (Sandbox Code Playgroud)
我的问题是如何通过其DLL访问我的C++应用程序中的某些功能.
我的C#应用程序中有3个表单 - Form1,Form2和Form3.目前,当我的应用程序启动时,它会加载Form1.我想在应用程序启动时打开所有三个表单.
我尝试这样做 Program.cs:
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
Application.Run(new Form2());
}
Run Code Online (Sandbox Code Playgroud)
但Form2仅在Form1关闭后显示.
如何在应用程序启动后立即同时显示所有3个表单?
假设我有一个这样的方法
void foo(const boost::shared_ptr<Pfoo>& rx)
{
myvector->push_back(rx);
}
Run Code Online (Sandbox Code Playgroud)
我读到当boost :: shared_ptr作为参考传递时,它的引用计数不会增加.现在,如果实际的ptr超出范围,上述情况会发生什么?
我最初使用以下方法 - 工作正常
var obj = Activator.CreateInstance("MyProject","MyProject.MyImpl");
Run Code Online (Sandbox Code Playgroud)
现在我在上面的行中收到错误,错误是:
Exception has been thrown by the target of an invocation.
Run Code Online (Sandbox Code Playgroud)
什么可能出错?
我目前有一个名为 getResult 的方法,它返回 const char* 现在我正在做这样的事情
if(getResult=="Something")
{
...
}
Run Code Online (Sandbox Code Playgroud)
然而,这种类型的比较似乎不起作用。“Something” 不也是一个 const char 指针吗?或者在这种情况下是比较地址?
我目前有以下课程,包括getter和setter
public class CustAccount
{
public string Account { get; set; } private string account;
public string AccountType { get; set; } private string accountType;
public CustSTIOrder(Account ord)
{
account = ord.Account;
accountType = ord.AccountType;
}
}
Run Code Online (Sandbox Code Playgroud)
现在我意识到,public string Account { get; set; }我不需要申报private string account.无论如何现在我的私有变量account包含值,但当我Account用来获取值时,我得到一个null.有关为什么我得到null的任何建议?
我最近发现,如果我有一个表单(比如from2),其中有一个公共委托声明(我知道委托没有附加到任何东西)
namespace SomeTest
{
public partial class Form2 : Form
{
public delegate void mydelegate(string some);
public Form2()
{ InitializeComponent();}
private void Form2_Load(object sender, EventArgs e)
{ }
}
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我传递该表单的实例来说明另一种形式(form1)
namespace SomeTest
{
public partial class Form1 : Form
{
Form2 fm = null;
public Form1(Form2 fm_)
{
this.fm = fm_;
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//Why cant fm access the public delegate ?
}
}
}
Run Code Online (Sandbox Code Playgroud)
为什么我们不能像fm.begininvoke(fm.mydelegate,"SomeParameter")我知道代表没有附加某些内容但我只是好奇为什么公共变量无法访问?