class

how to block usage of std::make_shared<T>

橙三吉。 提交于 2021-02-07 21:54:46
问题 I know I can prevent ordinary heap allocation of custom class and its descendants by making the class's operator new private, but is there any way to prevent a user of a library from calling std::make_shared on a custom class (or its descendants)? Apparently, simply making the operator new private in a class does not stop it. Note that I do not want to completely prevent shared pointers from being created by any means, as I intend to still be able to produce a std::shared_ptr for my custom

error: 'x' does not name a type

China☆狼群 提交于 2021-02-07 20:54:12
问题 When I try to declare an instance of my class 'Game' I receive the compile error "error: 'Game' does not name a type" for main.cpp. If probably doesn't matter but i'm using codeblocks. Relevant code from Game.cpp #include "../include/main.h" class Game { private: public: }; Relevant code from Main.cpp #include "../include/main.h" Game g; //this is the line it is referring to int main(int argc, char* args[]) { return 0; } I'm only starting to learn c++ so i probably overlooked something

make class only instantiable from specific class

落花浮王杯 提交于 2021-02-07 20:34:17
问题 Let's say I have 3 classes class1 , class2 and class3 . How can I have it that class1 can only get instantiated by class2 ( class1 object = new class1() ) but not by class3 or any other class? I think it should work with modifiers but I am not sure. 回答1: I want to rename your classes to Friend , Shy and Stranger . The Friend should be able to create Shy , but Stranger should not be able to. This code will compile: package com.sandbox; public class Friend { public void createShy() { Shy shy =

make class only instantiable from specific class

血红的双手。 提交于 2021-02-07 20:30:04
问题 Let's say I have 3 classes class1 , class2 and class3 . How can I have it that class1 can only get instantiated by class2 ( class1 object = new class1() ) but not by class3 or any other class? I think it should work with modifiers but I am not sure. 回答1: I want to rename your classes to Friend , Shy and Stranger . The Friend should be able to create Shy , but Stranger should not be able to. This code will compile: package com.sandbox; public class Friend { public void createShy() { Shy shy =

Powershell How to Overload Array Index Operator?

只谈情不闲聊 提交于 2021-02-07 14:14:50
问题 In Powershell, how do you overload the indexing of an array operator? Here is what I am doing now: class ThreeArray { $myArray = @(1, 2, 3) [int] getValue ($index) { return $this.myArray[$index] } setValue ($index, $value) { $this.myArray[$index] = $value } } $myThreeArray = New-Object ThreeArray Write-Host $myThreeArray.getValue(1) # 2 $myThreeArray.setValue(2, 5) Write-Host $myThreeArray.getValue(2) # 5 And, I want to do this: $myThreeArray = New-Object ThreeArray Write-Host $myThreeArray[1

Why does an array of classes consume ~20% more memory than array of structs?

佐手、 提交于 2021-02-07 13:36:53
问题 I'm making a 2D platformer game and to represent a level I'm using 2D array of tiles which are classes with fields for position, type and various flags. When I change the class key word in the tile class to struct , a loaded map consumes about 20% less memory. I don't know about rights and wrongs of this action, I just want to know why the difference in memory consumption. Edit: numbers are 1038 MB with tiles as classes and 845 MB as structs (without most of game data). 回答1: An array of

Can I avoid using the class name in the .cpp file if I declare a namespace in the header? [duplicate]

半腔热情 提交于 2021-02-07 12:48:48
问题 This question already has answers here : C++: “Class namespaces”? [duplicate] (4 answers) Closed 7 years ago . In C++, all I want to do is declare a DisplayInfo class in a .h file, and then in the .cpp file, not have to type the first DisplayInfo::DisplayInfo() and every function definition. Sadly, I've looked at over 20 topics and my C++ book for over two hours now and have not been able to resolve this. I think it's because I'm trying to use my 10-year-old java training in C++. 1st trial: /

Java referencing a class in the same directory

旧时模样 提交于 2021-02-07 11:42:30
问题 I created a Pair class in Java (similar to the c++ pair) and am having trouble referencing it from a different java file. I am working inside a Java file, let's call it fileA in the same directory as Pair.class. Additionally, I have written package current-directory at the top of both files. However, when I try javac fileA all my errors are cannot find symbol and the little arrow points to my custom Pair type. How do I get the java compiler to see Pair.class inside of fileA ? Thanks for all

How do you get the name of a generic class using reflection?

半城伤御伤魂 提交于 2021-02-07 09:59:45
问题 How do you get the name of a generic class using reflection eg public class SomeGenericClass<T> { } SomeGenericClass<int> test = new SomeGenericClass<int>(); test.GetType().Name returns "SomeGenericClass'1" How do I get it to return "SomeGenericClass" without the '1? 回答1: How about just the following? test.GetType().Name.Split('\'')[0] It works on non-generic classes too. 回答2: The '1 is part of the name, because, for example, List<T> and List (if I created such a class) are different classes.

Accessing different data members belonging to the same object from 2 different thread in C++

回眸只為那壹抹淺笑 提交于 2021-02-07 07:09:05
问题 I have a few objects I need to perform actions on from different threads in c++. I known it is necessary to lock any variable that may be used by more than one thread at the same time, but what if each thread is accessing (writing to) a different data member of the same object? For example, each thread is calling a different method of the object and none of the methods called modify the same data member. Is it safe as long as I don't access the same data member or do I need to lock the whole