static-cast

ambiguous call to overloaded function

六月ゝ 毕业季﹏ 提交于 2021-02-20 06:44:07
问题 I have two functions: void DoSomething( const tchar* apsValue ) void DoSomething( size_t aiValue ) Now I want to pass '0' as a size_t: DoSomething(0); The compiler throws an error: "ambiguous call to overloaded function" To solve this, I can use static_cast, for instance: DoSomething(static_cast<size_t>(0)); Or simple: DoSomething(size_t(0)); Is one of them better than the other? Are there any other approaches to solve this? 回答1: It's ambiguous because 0 has type int , not size_t . It can

ambiguous call to overloaded function

百般思念 提交于 2021-02-20 06:44:05
问题 I have two functions: void DoSomething( const tchar* apsValue ) void DoSomething( size_t aiValue ) Now I want to pass '0' as a size_t: DoSomething(0); The compiler throws an error: "ambiguous call to overloaded function" To solve this, I can use static_cast, for instance: DoSomething(static_cast<size_t>(0)); Or simple: DoSomething(size_t(0)); Is one of them better than the other? Are there any other approaches to solve this? 回答1: It's ambiguous because 0 has type int , not size_t . It can

How does implementing multiple COM interfaces work in C++?

为君一笑 提交于 2021-02-05 05:00:30
问题 I am trying to understand this example code regarding Browser Helper Objects. Inside, the author implements a single class which exposes multiple interfaces (IObjectWithSite, IDispatch). His QueryInterface function performs the following: if(riid == IID_IUnknown) *ppv = static_cast<BHO*>(this); else if(riid == IID_IObjectWithSite) *ppv = static_cast<IObjectWithSite*>(this); else if (riid == IID_IDispatch) *ppv = static_cast<IDispatch*>(this); I have learned that from a C perspective,

static_cast from base class pointer to derived class pointer is invalid

梦想的初衷 提交于 2020-04-16 04:51:13
问题 I am creating a simple test entity-component system. I have a base Component class with several derived classes. I then have several systems that apply some logic to these components. // Component.h // ------------ class Component { public: Component(); ~Component(); } // ControlComponent.h // ------------------- #include <string> #include "Component.h" class ControlComponent : public Component { public: std::string input = ""; // store simple input instruction ControlComponent();

What's the use of casting NULL to SomeType* in C++?

点点圈 提交于 2020-03-24 00:09:47
问题 I'm currently grinding through some third-party C++ code that looks rather odd to me (I started out with C++11). One of the many things that left me puzzled, are the many instances of static_cast from NULL to some pointer type: SomeClass* someClassPtr = static_cast<SomeClass*>(NULL); I know you can cast pointers e.g. from a base class pointer into a derived class pointer, but there is absolutely no inheritance going on here. As far as I can see, this should be enough: SomeClass* someClassPtr

Why is it allowed to static_cast a method of a derived class to a method of the base class?

核能气质少年 提交于 2020-01-24 19:33:10
问题 example struct B1{int x; void f(){x = 1;}}; struct D : B1{int x; void f(){B1::x = 2;}}; using Dmp = void(D::*)(); using B1mp = void(B1::*)(); int main() { Dmp dmp = &D::f; D d; (d.*dmp)(); // ok B1mp b1mp = static_cast<B1mp>(dmp); // hm, well that's weird B1 b1; (b1.*b1mp)(); dmp = &B1::f; // ok } And this example will compile and run just fine, and no problem will arise. But wait, now I'm going to use D::x in D::f , and now -- anything can happen at runtime. Yes, you can also static_cast a

Why do I Have to reinterpret_cast Pointer Pointers?

不羁的心 提交于 2020-01-24 10:00:06
问题 So this static_cast code is totally legal: int n = 13; void* pn = static_cast<void*>(&n); void** ppn = &pn; Yet this has to be made into a reinterpret_cast to compile: int n = 13; int* foo = &n; void** bar = static_cast<void**>(&foo); If I don't change it I get the error: error C2440: static_cast : cannot convert from int ** to void ** note: Types pointed to are unrelated; conversion requires reinterpret_cast , C-style cast or function-style cast So I take it the issue is "the types are

Reading in from a .txt file to a struct array that contains enum

拜拜、爱过 提交于 2020-01-17 12:14:04
问题 Here is my code enum Status {IN, OUT }; const int TITLE_SIZE = 50, ISBN_SIZE = 13, AUTHOR_SIZE = 25; struct Info { char title[TITLE_SIZE]; char isbn[ISBN_SIZE]; char author[AUTHOR_SIZE]; Status inOrOut; }; int main() { fstream dataFile; string filename; int numOfBooks = 0; Info *test = 0; int enumHolder = 0; cout << "How many books does the file contain? "; cin >> numOfBooks; test = new Info[numOfBooks]; cout << "Enter a file (with path) for input and output: "; cin >> filename; dataFile.open