小编Bra*_*don的帖子

委托构造函数C++

我这样做了吗?我正在尝试委托一个C++类构造函数,因为它基本上是相同的代码重复3次..我读了C++ x11并读到g ++ 4.7.2允许这样但我不确定我是否正在这样做对:

Bitmap::Bitmap(HBITMAP Bmp)
{
   //Construct some bitmap stuff..
}

Bitmap::Bitmap(WORD ResourceID)
{
   HBITMAP BMP = (HBITMAP)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(ResourceID), IMAGE_BITMAP, 0, 0, LR_SHARED);

   Bitmap(BMP);   //Delegates to the above constructor? Or does this create a temporary?
}
Run Code Online (Sandbox Code Playgroud)

或者我需要这样做:

Bitmap::Bitmap(HBITMAP Bmp)
{
   //Construct some bitmap stuff..
}

Bitmap::Bitmap(WORD ResourceID) : Bitmap((HBITMAP)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(ResourceID), IMAGE_BITMAP, 0, 0, LR_SHARED))
{
}
Run Code Online (Sandbox Code Playgroud)

c++ c++11

50
推荐指数
2
解决办法
4万
查看次数

为什么不从std :: allocator继承

我创建了自己的分配器,如下所示:

template<typename T>
class BasicAllocator
{
    public:
        typedef size_t size_type;
        typedef ptrdiff_t difference_type;
        typedef T* pointer;
        typedef const T* const_pointer;
        typedef T& reference;
        typedef const T& const_reference;
        typedef T value_type;


        BasicAllocator() throw() {};
        BasicAllocator (const BasicAllocator& other) throw() {};

        template<typename U>
        BasicAllocator (const BasicAllocator<U>& other) throw() {};

        template<typename U>
        BasicAllocator& operator = (const BasicAllocator<U>& other) {return *this;}
        BasicAllocator<T>& operator = (const BasicAllocator& other) {return *this;}
        ~BasicAllocator() {}

        pointer address (reference value) const {return &value;}
        const_pointer address (const_reference value) const {return …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance c++11

18
推荐指数
2
解决办法
5855
查看次数

在void指针上使用new

int main()
{
    void* Foo = new???
    delete Foo;
}
Run Code Online (Sandbox Code Playgroud)

你怎么做上面这样的事情?你不能放new void[size].我不想知道怎么做malloc()free().我已经知道这很有效.我很好奇,想知道如何用new和delete完成它.

我用Google搜索并看到了一些东西operator new(size); 和operator delete(size);

这些和new/ 之间有什么区别delete?为什么C++不仅仅允许新的void* [size]

c++

17
推荐指数
3
解决办法
2万
查看次数

99%CPU,3.51MB没有typedef

好的,所以我在标题中定义了大约500个函数指针,例如:

void (__stdcall *ptr_glAccum) (GLenum op, GLfloat value);
void (__stdcall *ptr_glActiveTextureARB) (GLenum texture);
void (__stdcall *ptr_glAlphaFunc) (GLenum func, GLclampf ref);
GLboolean (__stdcall *ptr_glAreTexturesResident) (GLsizei n, const GLuint *textures, GLboolean *residences);
void (__stdcall *ptr_glArrayElement) (GLint index);
void (__stdcall *ptr_glBegin) (GLenum mode);
void (__stdcall *ptr_glBindBufferARB) (GLenum target, GLuint buffer);
void (__stdcall *ptr_glBindTexture) (GLenum target, GLuint texture);
void (__stdcall *ptr_glBitmap) (GLsizei width, GLsizei height, GLfloat xorig, GLfloat yorig, GLfloat xmove, GLfloat ymove, const GLubyte *bitmap);
void (__stdcall *ptr_glBlendFunc) (GLenum sfactor, GLenum dfactor);
void …
Run Code Online (Sandbox Code Playgroud)

c++ c++11

15
推荐指数
1
解决办法
763
查看次数

将数组拆分为X长度的片段

目前我有一个大小为N的数组.我正在尝试从数组中复制每个X字节数.

例如,如果数组大小为10,我想要大小为3的数组.我将复制前3个元素,然后复制下3个元素和最后1个元素.

目前我正在使用以下算法:

int I = 0;
int sub = bytes.length;
int counter = 0;
for (I = 0; I < bytes.length; ++I) {
    if (I % 3 == 0 && I != 0) {
       NewArray[counter] = Arrays.copyOfRange(bytes, I - 3, I));
        sub -= 3;
        ++counter;
    }
}

NewArray[counter] = Arrays.copyOfRange(bytes, I - sub, I)); //Copy remainder.
Run Code Online (Sandbox Code Playgroud)

有没有更有效或更体面的方式来做我想要的?这个算法看起来很糟糕= l

我有什么想法可以改进它或者至少提示一下吗?

java arrays algorithm performance

12
推荐指数
2
解决办法
3万
查看次数

C++中的Unicode问题,但不是C

我正在尝试在Windows上用C++编写unicode字符串到屏幕上.我将控制台字体更改为Lucida Console,并将输出设置为CP_UTF8aka 65001.

我运行以下代码:

#include <stdio.h>  //notice this header file..
#include <windows.h>
#include <iostream>

int main()
{
    SetConsoleOutputCP(CP_UTF8);
    const char text[] = "??????";
    printf("%s\n", text);
}
Run Code Online (Sandbox Code Playgroud)

打印出来就好了!

但是,如果我这样做:

#include <cstdio>  //the C++ version of the header..
#include <windows.h>
#include <iostream>

int main()
{
    SetConsoleOutputCP(CP_UTF8);
    const char text[] = "??????";
    printf("%s\n", text);
}
Run Code Online (Sandbox Code Playgroud)

它打印: ????????????

我不知道为什么..

另一件事是我做的时候:

#include <windows.h>
#include <iostream>

int main()
{
    std::uint32_t oldcodepage = GetConsoleOutputCP();
    SetConsoleOutputCP(CP_UTF8);

    std::string text = u8"??????";
    std::cout<<text<<"\n";

    SetConsoleOutputCP(oldcodepage);
}
Run Code Online (Sandbox Code Playgroud)

我得到与上面相同的输出(非工作输出). …

c c++ unicode utf-8

12
推荐指数
1
解决办法
1911
查看次数

DYLD_LIBRARY_PATH和DYLD_INSERT_LIBRARIES无法正常工作

我创建一个.dylib文件并编译它:

#define _GNU_SOURCE
#include <dlfcn.h>
#include <stdio.h>

static void* (*real_malloc)(size_t);

void *malloc(size_t size)
{
    void *p = NULL;
    fprintf(stderr, "malloc(%zd) = ", size);
    p = real_malloc(size);
    fprintf(stderr, "%p\n", p);
    return p;
}

void __attribute((constructor))init()
{
    real_malloc = (decltype(real_malloc))dlsym(RTLD_NEXT, "malloc");
    if (NULL == real_malloc) {
        fprintf(stderr, "Error in `dlsym`: %s\n", dlerror());
        return;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后我创建一个调用的测试程序malloc.我确保对malloc的调用没有进行优化.

接下来我运行以下内容:

DYLD_PRINT_LIBRARIES=1 X=1 DYLD_INSERT_LIBRARIES=./libTestHook.dylib ./malloctest

它加载它但它根本不挂钩功能..任何想法?我在El Capitan升级之前尝试过这段代码并且它曾经工作过..我还让malloc抛出一个异常只是为了看它是否被调用.不是.

我错过了什么?

结果是:

sh-3.2# DYLD_PRINT_LIBRARIES=1 X=1 DYLD_INSERT_LIBRARIES=./libTestHook.dylib ./malloctest clear
dyld: loaded: /Users/Brandon/Desktop/./malloctest
dyld: loaded: ./libTestHook.dylib
dyld: loaded: /usr/lib/libc++.1.dylib …
Run Code Online (Sandbox Code Playgroud)

c macos osx-elcapitan

12
推荐指数
3
解决办法
7972
查看次数

阅读串口iOS

我有以下代码来读取和写入iOS 10.3.3越狱iPhone 6S上的串口(我使用h3lix进行越狱):

Serial.h:

//
//  Serial.h
//  iOUSB
//
//  Created by Brandon on 2018-05-21.
//  Copyright © 2018 XIO. All rights reserved.
//

#if !defined(__cplusplus)  //-fmodules -fcxx-modules
@import Foundation;
#else
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wauto-import"
#import <Foundation/Foundation.h>
#pragma clang diagnostic pop
#endif

#define BAUD_RATE 9600

@interface Serial : NSObject
+ (instancetype)shared;
- (bool)setup:(NSString *)filePath;
- (bool)disconnect;
- (size_t)available;
- (size_t)read:(uint8_t *)bytes length:(int32_t)length;
- (size_t)write:(uint8_t *)bytes length:(int32_t)length;
- (void)flushInputStream;
- (void)flushOutputStream;
- (void)flush;
- (NSString …
Run Code Online (Sandbox Code Playgroud)

serial-port objective-c root jailbreak ios10

11
推荐指数
1
解决办法
557
查看次数

JFrame删除任务栏图标

我有一个JFrame,我最小化托盘使用:

这用于显示:

Frame.this.Minimized = false;
Frame.this.setVisible(true);
systemTray.remove(systemTrayIcon);
Frame.this.setExtendedState(JFrame.NORMAL);
Run Code Online (Sandbox Code Playgroud)

这是为了隐藏:

if (SystemTray.isSupported()) {
    systemTray.add(systemTrayIcon);
    Frame.this.setVisible(false);
    Frame.this.Minimized = true;
}
Frame.this.setExtendedState(JFrame.ICONIFIED);
Run Code Online (Sandbox Code Playgroud)

但是,我不想将框架设置为不可见.当我将其设置为不可见时,它会删除我喜欢的任务栏图标.有没有办法删除框架的任务栏图标而不将可见性设置为false?

原因是当我最小化我的应用程序时,我可以发送命令并执行它们,但是第二个我将其可见性设置为false,它会停止执行来自外部应用程序的任何命令.我需要做的就是在最小化时从任务栏中删除图标,并在正常时显示图标.

有任何想法吗?

java swing

9
推荐指数
2
解决办法
5667
查看次数

const constexpr char*与constexpr char*

我知道const和constexpr之间的区别.一个是编译时常量,另一个是编译时或运行时常量.

但是,对于字符/字符串数组,我很困惑为什么编译器抱怨一个用于另一个.

例如,我有:

constexpr char* A[2] = {"....", "....."};

const constexpr char* B[2] = {"....", "....."};
Run Code Online (Sandbox Code Playgroud)

声明"A"我得到:

ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
Run Code Online (Sandbox Code Playgroud)

但声明"B"我没有得到任何警告.

为什么额外的const限定符会消除警告?他们两个都不是"const char*"吗?我问,因为两者都被声明,默认情况下constexpr应该使它成为一个const char*

我希望A没问题:S

c++ c++11

9
推荐指数
1
解决办法
3245
查看次数