When a class is defined as a private static, why do I need to make the get and set methods static?
Because you can't return a static member from an instance method.
It seems redundant to have to mark all members in a static class as static but C# requires that you do this. It's just the way the compiler was implemented.
As far as I know there are no members that inherit any modifiers from the type by default. In other words, a public class's members are not all public by default, etc. By requiring that you mark each member as static you are explicitly laying out the contract of the type.
Related
In the chapter Boilerplate code of GObject Manual, when ViewerFile is declared as a final type using G_DECLARE_FINAL_TYPE, how can we add public data to it since it is hidden behind the viewer-file.c which is not included?
The main distinction between a "derivable" GObject type and a "final" GObject type is the visibility of the instance data structure.
If the GObject type is "derivable" then you can only use a private instance data structure, as the instance structure is public and it's generated to just include the parent's structure.
If the GObject type is "final" then you only get instance fields, since the instance data structure is private to your C source file.
You cannot mix the two approaches, unless you decide not to use the macros and write the boilerplate by hand.
Additionally, you should not ever access fields on an instance data structure; provide accessor functions, instead, so that you can validate pre-conditions and post-conditions safely.
class MyClass : public CObject
{
public:
MyClass();
private:
CStringArray m_myArray;
};
causes error c2248
What's wrong with this ?
I think it's related to the fact that CStringArray and MyClass both derive from CObject.
Legacy class derived from CObject currently uses CustomArray it just seems wrong to me so I would like to replace it by CStringArray.
Microsoft compiler error C2248 means "Members of a derived class cannot access private members of a base class."
I can only assume you're trying to directly reference MyClass::m_myArray from somewhere in your code, without using a public accessor function.
Update
The real answer is that the problem is caused by the copy-constructor for MyClass, attempting to copy m_myArray, but it can't, because CObject derived classes aren't copyable by default. The solution would be to write a copy constructor yourself, and rather than attempt to copy the array, copy the contents one at a time, from the source array to the destination array. Either that or use std::vector (which you should be doing anyway, as MFC containers are horrible).
You are trying to access some private member of CObject. Since both your MyClass and CStringArray derive from CObject, without more context it is impossible to know the exact problem.
The only idea that comes to mind, basically because I've fallen in the trap many times, is that CObject's copy constructor is private, so if you are trying to copy either the CStringArray or your own class, explicit or implicitly, you'll get the error.
UPDATE:
I've just taken a look at CObject's declaration and the assignment operator, too, is private. Everything else is either public or protected.
Error C2248 says "Members of a derived class cannot access private members of a base class."
CStringArray class dosn't expose Copy constructor and assignment operator, look in code for such places and replace the code with elemenet by elemnet copy.
Why would I want a method that needs an instance? Why wouldn't I make all my methods static?
Why would you not want any state anywhere in your program?
Can you imagine if there were no String instances, and everything on String was static? How would you represent two distinct sequences of characters? Now apply the same logic to other code.
Fundamentally, OO languages are built around the idea of objects with state: one instance of Book isn't the same an another instance of Book - each Book instance encapsulates its name, author, publication date etc. How would you model that with only static methods, and no instances?
Of course you could make all your methods static and pass in a Book as the first parameter on each call that needed to use the state. Behind the scenes, something pretty much like that is already happening... except you've then lost polymorphism, so interfaces, abstract classes etc are useless. Not good.
Because objects are state and behavior together, encapsulated into a single component.
If you have individual instances, it means they can each have private data that varies from instance to instance.
Static data and methods are shared at the class level. Individual instances cannot have different static data.
Static methods can't directly access the member variables within an object - they can only access static variables.
If you had a car class and a static data member like an integer in it, you could only ever have one car because you cant make multiple instances of cars and get multiple instances of that variable - you'd only ever have the single static one.
Every car can't have the same license plate number, thus ever car needs its own license plate variable.
The methods in the class that work with that variable need to be non-static to work on it directly then.
Using the example of a "Car" class, you might have a method called "startCar()". Obviously, you want this method to interact only with a particular "instance" of a car and not be global to ALL your cars. Example in Java:
public class Car {
public void startCar() {
// code to start car
}
}
public class MyProgram {
public static void main(String[] Args) {
Car myFord = new Car();
Car myOpel = new Car();
myCar.startCar; // starts the Car "myCar" and leaves "myOpel" alone
}
}
It's also worth noting that Static Methods may not make use of Instance Variables of the class in which they are defined.
What is the importance of Static keyword in Java and in C++ and how it's functionality differ in both programming languages ?
Maybe this link will give you a better idea: http://www.pp.rhul.ac.uk/~george/PH2150/html/node48.html
It has a visual diagram that may make it easier to understand.
There are 2 meanings for static. The first if you have a static variable, this means there is only 1 instance of this variable. It works pretty much the same in all programming languages with the keyword.
A static function is a function that can be called, even if the class it resides in is not instantiated. Static functions are necessary in C# and Java because you cant declare functions in these languages which have no encompassing class.
in C++, you can declare functions in the global namespace. In this language, static functions are used to denote that a function belongs to the class, but you dont have to instantiate the class to use the function. You could use a static function to access private variables of the class. Also note that in C++, static functions have a known memory address, so you can use function pointers to point to them without instantiating the class.
For Java, Understanding Instance and Class Members is a good place to start.
For C++, Microsoft has a reference on the static keyword.
There are many readily available programming language resources that will help you understand what the static keyword means. The above are two of them that I found with a quick Google search.
Use static for fields and methods that can only have one instance. That means they are not relevant to instances of a class, but to the class itself. For example the main thread (public static void main).
It works the same way in both languages. I assume you know what's object-oriented programming, and what's the difference between classes and objects/instances. So, if you mark a method or variable as "static", it operates on a class level, not instance level. All objects/instances share the same value of the "static" variable.
Is there any way to get the equivalent of GetType within a static constructor?
I want to iterate through the available properties of the type within the static constructor but GetType is an instance method? Why is this? The typeinfo should exist in the static context. Is there a way around this?
Just use
Type type = typeof(TheCurrentType);
It should never be more complex than this since you always know the actual type; there's no polymorphism to deal with in static methods.
I don't think you can get derived types (other than by iterating through all types to see what derives from the current type). To get the current type, you can:
Type currentType = (new StackFrame()).GetMethod().DeclaringType;