如果我有一个类,让我们称之为class A
,它是从一个名为class B
的类派生的,是否可以从我的派生中返回class B
的对象class A
的对象?
答案 0 :(得分:1)
如果我有一个类,让我们称之为
Class A
,它是从一个名为Class B
的类派生的,是否可以从我的派生中返回Class B
的对象Class A
的对象?
确定可能:
class B {
// ...
};
class A : public B {
public:
// Not that this is explicitly necessary:
B& getAsB() { return *this; }
};
简单地写一下也是有效的:
A a;
B& b = a;
虽然做了类似
的事情A a;
B b = a;
将创建副本并将原始A
切片为B
。
答案 1 :(得分:-2)
是的,这是可能的,但它在任何方面都不显着。
Microsoft.Office.Interop.Outlook.Attachment attachment = msg.Attachments.Add(@"D:\Users\chart.jpeg", OlAttachmentType.olEmbeddeditem, null, "Some image display name");
attachment = msg.Attachments.Add(@"D:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg", OlAttachmentType.olEmbeddeditem, null, "Some image display name");
attachment = msg.Attachments.Add(@"D:\Users\Public\Pictures\Sample Pictures\Hydrangeas.jpg", OlAttachmentType.olEmbeddeditem, null, "Some image display name");
attachment = msg.Attachments.Add(@"D:\Users\Public\Pictures\Sample Pictures\Penguins.jpg", OlAttachmentType.olEmbeddeditem, null, "Some image display name");
string imageCid1 = "image001.jpg@123";
string imageCid2 = "image002.jpg@123";
string imageCid3 = "image003.jpg@123";
string imageCid4 = "image004.jpg@123";
attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001E", imageCid1);
attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001E", imageCid2);
attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001E", imageCid3);
attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001E", imageCid4);
msg.HTMLBody = String.Format("<body><img src=\"cid:{0}\"><br/><img src=\"cid:{1}\"><br/><img src=\"cid:{2}\"><br/><img src=\"cid:{3}\"></body>", imageCid1, imageCid2, imageCid3, imageCid4);
这里,即使没有继承,类struct B {}; // using struct instead of class for convenience
struct A // may or may not inherit from B
{
B ReturnSomeObject()
{
return B();
}
};
int main()
{
A object1;
B object2 = object1.ReturnSomeObject();
}
中的方法也可以返回类A
的对象。如果添加继承
B
它仍然有效。