#include <stdio.h>
class Foo {
public:
Foo(char x);
Foo(char x, int y);
~Foo();
void abc();
void dev();
};
void Foo::dev()
{
printf("inside dev \n");
}
void Foo::abc()
{
printf("inside abc \n");
delete this;
dev();
}
Foo::Foo(char x)
{
printf("inside 1 argu const---------------");
}
Foo::~Foo()
{
printf("inside 1 argu dest---------------");
}
#include "test.h"
int main()
{
Foo *obj=new Foo('a');
printf("%u inside main\n", obj);
obj->abc();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在查看程序的输出之后,似乎仍然调用"dev"函数,尽管在调用dev之前在函数abc中调用了"delete this"?gcc/g ++如何处理这个?
我一直想知道是否有可能将另一个对象分配给$ this?
在CodeIgniter中,我从主控制器调用另一个控制器.
应用/控制器/ module.php
Class Module extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function _remap($class, $args = array()) {
// doing some checks that is there a valid file, class and method
// assigning the $method.
// including MY_Module class. I'am extending every module from this class.
// then:
$EG = new $class();
call_user_func_array(array(&$EG, $method), array_slice($this->uri->rsegments, 3));
}
}
Run Code Online (Sandbox Code Playgroud)
在被叫班级:
Class Users extends MY_Module
{
public function __construct() {
parent::__construct();
}
public function index() {
// …Run Code Online (Sandbox Code Playgroud) 我刚刚编写了一个示例程序来查看删除它的行为
class A
{
~A() {cout << "In destructor \n ";}
public:
int a;
A() {cout << "In constructor \n ";}
void fun()
{
cout << "In fun \n";
delete this;
cout << this->a << "\n"; // output is 0
this->fun_2(); // how m able to call fun_2, if delete this is called first ??
}
void fun_2()
{
cout << "In fun_2 \n";
}
main()
{
A *ptr = new A;
ptr->a = 100;
ptr->fun(); //delete this …Run Code Online (Sandbox Code Playgroud) 我想将$(this)传递给函数,但我不确定.有一个类似的线程,但我仍然无法使其工作.我希望有人可以帮助我.
$(document).ready(function() {
var delay = (function(){
var timer = 0;
return function(callback, ms){
clearTimeout (timer);
timer = setTimeout(callback, ms);
};
})();
$('input').keyup(function() {
delay(function(){
alert($(this).val());
}, 1000 );
});
});
Run Code Online (Sandbox Code Playgroud) 假设我有一个班级:
class test {
public:
void print();
private:
int x;
};
void test::print()
{
cout<< this->x;
}
Run Code Online (Sandbox Code Playgroud)
我有这些变量定义:
test object1;
test object2;
Run Code Online (Sandbox Code Playgroud)
当我打电话object1.print() this时碰巧存储地址,object1所以我x从object1打印得到,当我打电话object2.print() this时,恰好是存储地址,object2我x从object2打印到.怎么会发生?
我想问一下,我们将如何实现一个类的复制构造函数,该类具有自身指针作为其数据成员,我想实现深层复制,
class City
{
string name;
City* parent;
public:
City(string nam, double dcov);
City(string nam, double dcov, City* c);
City(const City& obj)
{
this-> name = obj.name;
// how to assign parent
parent = new City(??)
}
~City();
void setName(string name1);
void setDistanceCovered(int dist);
string getName();
double getDistanceCovered();
City* getParent(){return parent;}
};
Run Code Online (Sandbox Code Playgroud)
我很困惑,这行// how to assign parent
parent = new City(??)会再次调用构造函数而不是深层复制?问候.
getA()&getB()和setA()&setB()之间有什么区别吗?
如果它们是相同的,这是首选语法?
class A{
public:
int x;
int getA(){return x;}
int getB(){return this->x;}
void setA(int val){ x = val;}
void setB(int val){ this->x = val;}
};
int main(int argc, const char * argv[]) {
A objectA;
A objectB;
object.setA(33);
std::cout<< object.getA() << "\n";
objectB.setB(32);
std::cout<< object.getB() << "\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud) 如果我在 lambda 中捕获“this”-ptr,我就可以毫无问题地调用成员函数。但是,当我明确捕获指针(不提及“this”)时,它会停止工作。难道我做错了什么?根据我的理解,指针应该是一样的,所以这真的让我很惊讶。是否有一些编译器魔法以特殊的方式处理“this”?
#include <cstdio>
#include <string>
struct client
{
auto foo(std::string&& other)
{
printf("%s!\n", other.data());
}
void local()
{
std::string str = "Hello World this is a sentence to long for sso!";
auto lambda = [this, other = std::move(str)]() mutable {
foo(std::move(other));
}();
}
static auto external(void* ptr) {
std::string str = "Hello World this is a sentence to long for sso!";
client* conv_ptr = static_cast<client*>(ptr);
auto lambda = [conv_ptr, other = std::move(str)]() mutable {
foo(std::move(other));
}();
} …Run Code Online (Sandbox Code Playgroud) 我很难找到这方面的资源,但是,当我在最新的 Java (21) 上编译代码时,我的很多类都遇到了这个错误。
这是一个代码示例。
public class ThisEscapeExample
{
public Object o;
public ThisEscapeExample()
{
this.overridableMethod();
}
public void overridableMethod()
{
this.o = new Object();
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的编译命令。
javac -Xlint:all ThisEscapeExample.java
ThisEscapeExample.java:9: warning: [this-escape] possible 'this' escape before subclass is fully initialized
this.overridableMethod();
^
1 warning
Run Code Online (Sandbox Code Playgroud)