member


can memcpy() be used to change “const” member data?

核能气质少年 提交于 2020-02-02 03:30:07
问题 For a struct with const members struct point { const int x; const int y; }; that is used as member data struct Foo { point pt{ 0, 0 }; void move_x(int value); }; How can Foo::move_x() be written to update Foo::pt ? Is it OK to use memcpy() ? #include <memory.h> void Foo::move_x(int value) { const point pt_{ pt.x + value, pt.y }; (void) memcpy(&pt, &pt_, sizeof(pt_)); // pt = pt_; } This can be safely done using a pointer #include <memory> struct Bar { std::unique_ptr<point> pPt_{ new point{ 0

Get Member/Fields of an existing Object

梦想与她 提交于 2020-01-25 16:56:23
问题 i will discribe my problem with the following example: public class Person{ private int age; private String name; public Person(int age, String name){ this.age = age; this.name = name; } } I ve a class with some Members (age and name in this case) but i don't know which and how much my class does have. Also i don't even care about the amount or the types. I wan't to get all members of only one class. like this: private List<Object> getAll(Class searchedClass, Object from){ // This is where

Get Member/Fields of an existing Object

徘徊边缘 提交于 2020-01-25 16:54:32
问题 i will discribe my problem with the following example: public class Person{ private int age; private String name; public Person(int age, String name){ this.age = age; this.name = name; } } I ve a class with some Members (age and name in this case) but i don't know which and how much my class does have. Also i don't even care about the amount or the types. I wan't to get all members of only one class. like this: private List<Object> getAll(Class searchedClass, Object from){ // This is where

Why can't I modify class member variable outside any methods? [duplicate]

懵懂的女人 提交于 2020-01-22 03:44:05
问题 This question already has answers here : Syntax error on token(s) “product1. ”VariableDeclaratorId expected after this token (1 answer) syntax error on tokens, variableDeclarator expected instead (3 answers) syntax error on addActionListener (3 answers) I have an error with system.out.print and I don't know why? [closed] (7 answers) Syntax error on token(s), misplaced construct(s) (4 answers) Closed 2 years ago . I have a class with some variables. When I instantiate an object of that class

Is `this` keyword optional when accessing members in C#?

。_饼干妹妹 提交于 2020-01-21 11:01:34
问题 I notice that if you have a private member in a class, you can access it in the class methods by just referring to it's name. You do not need to say this.memberName , just memberName works. So is the this keyword optional in the context of member access? I do see it is useful when you want to clarify the scope - when you have 2 variables with the same name. Is there any other reason to use it when accessing members? 回答1: Yes, it's optional. The only times you'd have to use it are when you

Is `this` keyword optional when accessing members in C#?

妖精的绣舞 提交于 2020-01-21 11:01:07
问题 I notice that if you have a private member in a class, you can access it in the class methods by just referring to it's name. You do not need to say this.memberName , just memberName works. So is the this keyword optional in the context of member access? I do see it is useful when you want to clarify the scope - when you have 2 variables with the same name. Is there any other reason to use it when accessing members? 回答1: Yes, it's optional. The only times you'd have to use it are when you

C++ class member check if not a template

一世执手 提交于 2020-01-15 11:14:54
问题 template instantiation check for member existing in class explains how to check if a class member exists in a template. However, given a set of processes within a switch ( NOT a template) is there a way to handle a member check case. It should be similar to something like this. Note that the actual class definition is not under my control and is being created in a future release of header files and libraries that I am using. I am aware that this preprocessor example would not work, but since

public interface member in c#8

限于喜欢 提交于 2020-01-15 03:10:51
问题 Since the latest version of c#, it is possible to write the following interface: public interface IMyInterface { public void MyMethod(); } This seems like a code smell to me, as I feel like the intention was to write the previously available: public interface IMyInterface { void MyMethod(); } Are those two interfaces exactly the same ? Does the public keyword add/changes anything ? Is this something that should be corrected, or am I wrong and should public be consistently used now ? 回答1:

method overriding Vs class variable overriding in java

℡╲_俬逩灬. 提交于 2020-01-14 19:53:27
问题 I was just trying some sample code for checking class variable overriding behavior in Java. Below is the code: class A{ int i=0; void sayHi(){ System.out.println("Hi From A"); } } class B extends A{ int i=2; void sayHi(){ System.out.println("Hi From B"); } } public class HelloWorld { public static void main(String[] args) { A a= new B(); System.out.println("i->"+a.i); // this prints 0, which is from A System.out.println("i->"+((B)a).i); // this prints 2, which is from B a.sayHi(); // method

enum constructors (creating members of members)

人走茶凉 提交于 2020-01-14 14:40:48
问题 In D, I'm trying to create an enum whose members have members. I can better explain what I'm trying to do with an example, where s and i stand in for the sub-members I'm trying to create: In Python, I can do this: class Foo(enum.Enum): A = "A string", 0 B = "B string", 1 C = "C string", 2 def __init__(self, s, i): self.s = s self.i = i print(Foo.A.s) Java can do something like this: public enum Foo { A("A string", 0), B("B string", 1), C("C string", 2); private final String s; private final

工具导航Map