我有以下代码:
bool SendClientRequest(Action<ICustomerService> channel)
{
string proxy ="my.proxy.domain:8080";
string user = "user1";
string password="secret";
// maybe i do not need this 3 lines!
WebProxy webproxy = new WebProxy(proxy, true);
webproxy.Credentials = new NetworkCredential(user, password);
WebRequest.DefaultWebProxy = webproxy;
CustomBinding customBinding = new CustomBinding();
customBinding.Elements.Add(new HttpTransportBindingElement()
{
AuthenticationSchemes.None : AuthenticationSchemes.Basic,
ProxyAddress = string.IsNullOrEmpty(proxy) ? null : new Uri(proxy),
UseDefaultWebProxy = false,
BypassProxyOnLocal = true,
TransferMode = TransferMode.Streamed,
MaxReceivedMessageSize = 84087406592,
MaxBufferPoolSize = 0x1000000,
MaxBufferSize = 0x1000000
});
using (ChannelFactory<ICustomerService> factory = new
ChannelFactory<ICustomerService>(customBinding ))
{
IClientChannel contextChannel = null;
string url = "http://my.domain.de/Distribution/eService.svc",
EndpointAddress ep = new EndpointAddress(url);
ICustomerService clientChannel = factory.CreateChannel(ep);
contextChannel = clientChannel as IClientChannel;
contextChannel.OperationTimeout = TimeSpan.FromMinutes(rcvTimeout );
channel(clientChannel); // <- here i get the exception!
return true;
}
}
到目前为止一直很好,但我在以下代码中遇到错误Class A {
public:
A::A(const char* name):
_name(name)
{}
virtual void doSomething();
private:
const char* _name;
}
Class B : public A {
B::B(const char* name):
A(name)
{}
void doSomething() {
//do something
}
}
:
crosses initialization of B* newB
为什么会出错?
一些背景:
我想在向量中存储派生类的指针,以便于搜索/迭代。名称是唯一的,因此我可以遍历向量并查找指向具有正确名称的对象的指针。从指向对象调用方法时,应该调用继承的方法(std::vector<A*> vectorOfAs;
switch (enumType) {
case enum1 :
B* newB = new B("foobar");
vectorOfAs.push_back(newB);
break;
case enum2 :
C* newC = new C("barfoo");
vectorOfAs.push_back(newC);
break;
}
)。
编辑:@FrançoisAndrieux:你是对的。做了(严重的)错字。
已将//do something
更改为Class B : public B
答案 0 :(得分:1)
newB
在switch语句的范围内,这使得它在所有交换机情况下都可用,但不会在所有这些情况下初始化。您应将每个案例放在其自己的本地范围内(有关详细信息,请参阅this answer):
switch (enumType) {
case enum1 :
{
B* newB = new B("foobar");
vectorOfAs.push_back(newB);
break;
}
case enum2 :
{
C* newC = new C("barfoo");
vectorOfAs.push_back(newB);
break;
}
}
此时你会在enum2中遇到一个编译器错误(从而暴露出一个错误),因为你推动newB而不是newC,这就是我认为你想要的:
switch (enumType) {
case enum1 :
{
B* newB = new B("foobar");
vectorOfAs.push_back(newB);
break;
}
case enum2 :
{
C* newC = new C("barfoo");
vectorOfAs.push_back(newC); // <--
break;
}
}
这应该有用。