我在派生类的私有函数中使用的基类的受保护构造函数有问题:
基类:
class Socket
{
public:
virtual ~Socket();
// Constructors :
Socket();
protected:
Socket(SOCKET& s);
};
Run Code Online (Sandbox Code Playgroud)
派生类:
class Server : public Socket
{
public:
Server();
~Server();
private:
int ServerLoop();
};
Run Code Online (Sandbox Code Playgroud)
我尝试在 ServerLoop 函数中创建 Socket 对象
SOCKET client_sock = accept( m_socket, ( sockaddr* )&client_addr, &size );
Socket* Client = new Socket( client_sock );
^^^^^^
Run Code Online (Sandbox Code Playgroud)
但我收到此错误:
error C2248: 'NET_SOCKETS::Socket::Socket' : cannot access protected member
declared in class 'NET_SOCKETS::Socket'
Run Code Online (Sandbox Code Playgroud)
在 ^^^^ 的行中。是什么导致了这个错误?
在下面的课程中,我声明 myMap
public class AllMap {
public static final Map<String, String> myMap= new HashMap<>();
static {
Map.put("yy", "AA");
Map.put("xx", "BB");
}
}
Run Code Online (Sandbox Code Playgroud)
我需要访问其他班级的地图。
public class Test {
FieldMap.Map;
}
Run Code Online (Sandbox Code Playgroud)
一切正常,但声纳在第一堂课上发出警告:
使该成员“受保护”。
在线上
public static final Map<String, String> myMap = new HashMap<>();
Run Code Online (Sandbox Code Playgroud)
我应该忽略此警告还是应该将其更改为受保护?
#include <iostream>
#include <cmath>
using namespace std;
class Point
{
protected:
double x,y,z;
double mag;
public:
Point(double a=0.0, double b=0.0, double c=0.0) : x(a), y(b), z(c)
{
mag = sqrt((x*x)+(y*y)+(z*z));
}
friend ostream& operator<<(ostream& os, const Point& point);
};
ostream& operator<<(ostream& os, const Point& point)
{
os <<"("<<point.x<<", "<<point.y<<", "<<point.z<<") : "<<point.mag;
return os;
}
class ePlane : public Point
{
private:
Point origin;
public:
static double distance(Point a, Point b);
ePlane() : origin(0,0,0){}
};
double ePlane::distance(Point a, Point b) …Run Code Online (Sandbox Code Playgroud) 我正在将代码从 PHP 移植到 NodeJs(Typescript)。\n我遇到了以下 PHP 代码(简化)\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2 \xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0
\n\n<?php\nclass A {\n protected function protectedData() {\n return \'accessible\';\n }\n}\nclass B extends A {\n public function extractTest($anInstanceOfA) {\n return $anInstanceOfA->protectedData();\n }\n}\n$instanceA = new A();\n$instanceB = new B();\necho $instanceB->extractTest($instanceA);\nRun Code Online (Sandbox Code Playgroud)\n\n在沙箱中运行它会产生“可访问”的回显。
\n\n我在 Typescript 中编写了相同的代码,但这似乎不起作用......
\n\nclass A {\n protected protectedData(): string {\n return \'accessible\';\n }\n}\n\nclass B extends A {\n public extractTest(anInstanceOfA: A): string {\n return anInstanceOfA.protectedData();\n }\n}\n\nconst instanceA = new A();\nconst instanceB = new B();\n\n\nconsole.log(instanceB.extractTest(instanceA));\nRun Code Online (Sandbox Code Playgroud)\n\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0
\n\n\n错误:属性“protectedData”受保护,只能通过类“B”的实例访问。(2446)\n \xc2\xa0
\n …
给定Ada保护类型:
protected type A is
procedure Foo;
...
private
M : Map;
...
end A;
Run Code Online (Sandbox Code Playgroud)
在完成受保护对象时,您将如何实现或模拟调用的Finalize过程?
基本上我需要使用受保护类型的私有成员进行一些管理(迭代某些地图等).
我有一个带有受保护变量的抽象类
abstract class Beverage
{
protected string description;
}
Run Code Online (Sandbox Code Playgroud)
我无法从子类访问它.Intellisense不会显示它可访问.为什么会这样?
class Espresso:Beverage
{
//this.description ??
}
Run Code Online (Sandbox Code Playgroud) 在ActionScript 3中有一种方法(一个黑客 - 可能通过方括号,可能通过克隆一个对象,可能通过原型,可能通过命名空间,......)来改变一个私有或受保护的类成员?
例如,如果我有一个IconToast由someLibrary.swc提供的类,我知道它有一个
protected var windowOptions:WindowOptions;
Run Code Online (Sandbox Code Playgroud)
我能以某种方式改变它吗?我尝试了很多东西,例如:
var errorToast:IconToast = new IconToast();
errorToast.addButton("Dismiss");
errorToast.message = "Error when connecting";
errorToast['windowOptions'].timeout = 10 * 1000;
errorToast.show();
Run Code Online (Sandbox Code Playgroud)
(给我运行时错误ReferenceError:错误#1069:在IconToast上找不到属性windowOptions并且没有默认值).
actionscript private protected actionscript-3 private-members
我正在尝试一个简单的C++继承示例.但我无法理解.当我尝试B从类继承的类的受保护成员时,A它表示A::baz受保护.
#include <iostream>
class A {
public:
int foo;
int bar;
protected:
int baz;
int buzz;
private:
int privfoo;
int privbar;
};
class B : protected A {}; // protected members go to class B, right?
int main() {
B b;
b.baz; // here is the error [A::baz is protected]
}
Run Code Online (Sandbox Code Playgroud)
我似乎无法找到我做错的事.我试图改变class B : protected A到: public A,但它仍然无法正常工作.
我class Dad提供了一些protected孩子需要的方法,即使它实际上并不知道这些孩子是谁.
class Dad
{
protected:
void method()
{
// some amazing stuff (I swear)
};
};
Run Code Online (Sandbox Code Playgroud)
实际的继承class Child: public Dad,在当前实现我的计划,已决定将自己推断为几类class GrandKid1: Child,class GrandKid2: Child等等.
但是,为了安全和组织起见,Child更喜欢孙子method()们不能自己打电话.我如何阻止他们这样做?
显然,以下天真代码会产生链接器错误:
class Child: public Dad
{
private:
void method();
};
Run Code Online (Sandbox Code Playgroud)
如何Child阻止protected成员传播method()到自己的派生类?
当我们写下面的代码
Stream.of(1,2,3,4,5).filter(i -> (i%2 == 0)).map( i -> i*i );
Run Code Online (Sandbox Code Playgroud)
表达式i -> (i%2 == 0)或i -> i*i将变成私有方法.
在我的用例中,编写了一个junit测试以确保没有方法是私有的(是啊,那是强制的),并且这些lambda表达式失败了.
有人可以提出一些建议,其中我不必更改junit来为lambda表达式添加一些排除,但是让这些表达式在内部创建受保护的方法吗?
protected ×10
c++ ×4
inheritance ×3
java ×2
private ×2
.net ×1
actionscript ×1
ada ×1
c# ×1
c++11 ×1
constructor ×1
java-8 ×1
lambda ×1
oop ×1
restriction ×1
sonarqube ×1
types ×1
typescript ×1