请参阅以下代码:
#include<iostream>
#include<stdlib.h>
using namespace std;
class ex
{
int i;
public:
ex(int x){
i=x;
cout<<"\nconstructor";
}
void setval(int x)
{
i=x;
}
int geti(){return i;}
~ex()
{
cout<<"\ndestructor";
}
};
int main()
{
ex *ob;
ob=(ex*) malloc(sizeof(ex));
ob->setval(5);
cout<<ob->geti();
delete ob;
}
Run Code Online (Sandbox Code Playgroud)
我以为上面的代码会显示错误,但它编译成功并显示输出
5
destructor
Process returned 0 (0x0) execution time : 0.270 s
Run Code Online (Sandbox Code Playgroud)
我的问题是:
我可以使用 为对象分配内存malloc吗?
如何malloc为没有构造函数参数的类分配内存?
我可以malloc()用于分配和delete解除分配还是new用于分配和free()解除分配?
没有构造函数如何调用析构函数?
我最近查看了 jpackage 有任何选项会自动将应用程序添加到启动中,例如考虑我有,
应用程序.java
package org.openjfx;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
/**
* JavaFX App
*/
public class App extends Application {
@Override
public void start(Stage stage) {
var label = new Label("Hello, JavaFX");
var scene = new Scene(new StackPane(label), 640, 480);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
Run Code Online (Sandbox Code Playgroud)
模块信息.java
module Sample {
requires javafx.controls;
opens org.openjfx;
}
Run Code Online (Sandbox Code Playgroud)
使用maven生成运行时,
mvn javafx:jlink
Run Code Online (Sandbox Code Playgroud)
然后生成安装程序,
jpackage --win-dir-chooser --runtime-image ./target/image/ --name Sample-Javafx --module Sample/org.openjfx.App …Run Code Online (Sandbox Code Playgroud) 我目前正在学习运行时类型 ID 和 Casting Operator。我有一些问题,你能帮我解决这个疑惑吗?
请参阅以下代码:
#include <iostream>
#include <typeinfo>
using namespace std;
class base
{
};
class derived
{
};
int main()
{
cout<<typeid(char).name()<<endl;
cout<<typeid(int).name()<<endl;
cout<<typeid(float).name()<<endl;
cout<<typeid(double).name()<<endl;
cout<<typeid(base).name()<<endl;
cout<<typeid(derived).name()<<endl;
}
Run Code Online (Sandbox Code Playgroud)
输出:
c
i
f
d
4base
7derived
Process returned 0 (0x0) execution time : 0.238 s
Press any key to continue.
Run Code Online (Sandbox Code Playgroud)
问题:
typeid(base).name()给出“ 4base”;4这里有什么并typeid(derived).name()给出“ 7derived”;这是7什么?
为什么typeid(char).name()和其他内置数据类型只给出第一个字母?
功能是什么type_info::before()?
感谢您的时间和回答。
考虑以下代码:
#include<iostream>
enum week
{
sun=1,
mon,
tue,
wed,
thr,
fri,
sat
};
week &operator++(week& day,int)
{
if(day==sat)
day=sun;
else
day++; // This expression
return day;
}
int main()
{
week day=sun;
for(int i=0;i<=10;i++,day++)
{
std::cout<<day;
}
}
Run Code Online (Sandbox Code Playgroud)
在表达式中day++它进入无限递归。
如果我像((int)day)++编译器一样投射它会出现以下错误:
error: lvalue required as increment operand
Run Code Online (Sandbox Code Playgroud)
如果我将线路更改为day=week(((int)day)+1)它的工作原理。但是如何修复上面的代码以便它与++操作员一起工作?
请看下面的代码:
#include<iostream>
#include<stdlib.h>
#include<new>
using namespace std;
class ex
{
int x;
public:
ex():ex(0){}
ex(int x):x(x)
{
//cout<<"\nConstructor";
}
void *operator new(size_t siz)
{
void *p;
cout<<"\nOverloaded new operator normal version";
p=malloc(siz);
if(p==NULL)
{
bad_alloc ba;
throw ba;
}
else
return p;
}
void operator delete(void *p,size_t sz)
{
cout<<"\nOverloaded delete normal version:"<<sz;
free(p);
}
void *operator new(size_t siz,const nothrow_t &tag)
{
void *p;
cout<<"\nOverloaded new operator nothrow version";
p=malloc(siz);
if(p==NULL)
{
return 0;
}
else
return p;
}
void …Run Code Online (Sandbox Code Playgroud) 在C语言中,stdout并stderr都打印到系统默认的控制台窗口。有什么区别stderr和stdout其他比缓冲的水平?
请参阅以下代码:
#include<iostream>
using namespace std;
class ex
{
int i;
public:
ex(int x){i=x;}
void operator-()
{
i=-i;
}
int geti(){return i;}
};
class derived:public ex
{
int j;
public:
derived(int x,int y):ex(x){j=y;}
void operator-()
{
j=-j;
}
int getj(){return j;}
};
int main()
{
derived ob(1,2);
-ob;
cout<<ob.geti();
cout<<"\n"<<ob.getj();
}
Run Code Online (Sandbox Code Playgroud)
输出:
1
-2
Process returned 0 (0x0) execution time : 0.901 s
Press any key to continue.
Run Code Online (Sandbox Code Playgroud)
我-在基类和派生类中都定义了运算符,但-ob;只调用派生类的运算符。那么如何将i字段更改为-i(调用基类中的运算符)。
我需要任何显式函数来实现这一点吗?
我目前正在学习 c++11 我不明白 std::initializer_list 的构造函数它看起来像这样
constexpr initializer_list() noexcept : _First(nullptr), _Last(nullptr) {}
constexpr initializer_list(const _Elem* _First_arg, const _Elem* _Last_arg) noexcept
: _First(_First_arg), _Last(_Last_arg) {}
Run Code Online (Sandbox Code Playgroud)
但它是如何工作的
std::initializer_list<int> v{1,2,3,4,5,6,7,8,9,0};
Run Code Online (Sandbox Code Playgroud)
我试过这个
constexpr init(const _Elem* _First_arg, const _Elem* _Last_arg) noexcept
: _First(_First_arg), _Last(_Last_arg) {}
Run Code Online (Sandbox Code Playgroud)
但这显示错误
init<int> ob{1,2,3,4,5,6,7,8,9,0}; //this shows error
note: candidate: 'constexpr init<_Elem>::init(const _Elem*, const _Elem*) [with _Elem = int]'
constexpr init(const _Elem* _First_arg, const _Elem* _Last_arg) noexcept
^~~~
note: candidate expects 2 arguments, 10 provided
Run Code Online (Sandbox Code Playgroud)
我把 {} 改为 () 就像
std::initializer_list<int> v(1,2,3,4,5,6,7,8,9,0); …Run Code Online (Sandbox Code Playgroud)