C Sharp :: How To Use Static Method And Datatype In A Class
Apr 3, 2013How to use static in a class, function and variable.
View 1 RepliesHow to use static in a class, function and variable.
View 1 RepliesMy Fraction.h class looks like :
class Fraction {
int num;
unsigned int den;
public:
Fraction(int = 1,int =1);
//Constants of Datatype
[Code] ....
The implementation Fraction.cpp is as follows :
#include "Fraction.h"
Fraction::Fraction(int n, int d):num(n),den(d){
cout << This is double param constructor <<endl;
}
And the application main.cpp is
int main(){
Fraction f1(3,9);
f1 = Fraction::sc_fUnity; // how to implement this ?
}
How can I write the Fraction.cpp for the constant static member ?
Have following code:
class Program
{
static void Main(string[] args)
{
[Code]....
My question according to what i just wrote:
1. Is that mean that Do() is only available for use by Dog itself because Dog is 'oryginal' Dog, and if i create new dogs - instances of oryginal Dog (dog1, dog2 ...) they cant access because Do is only available fo 'oryginal' one? Is that correct thinking?
2. If i would want to have something common (e.g value) for all dogs is that good way to create static field/method for Dog instead of non-static once then all instances of Dog would access Dog static member to get/change it? Just stupid example: static method GetAmountOfLegs() which return 4 Then all instances can take/call that value from Dog. Is that correct thinking?
what is static method?
why we can use static method inc#?
I am writing my program on C++ language. I have one promblem. I need to set signal handler for my process. As the signal is related with the process on system level I have faced the problem.
My program consists several classes. They are connected together. But it doesn't metter in this case. The problem is that I need to access to member and methods of the class from my signal handler. For instance , I have class named Foo at it has some members and methods.
So from my handler I need to call its function and change members.
I understand that compiler should know that this class instances will exist during all program execution.
I have tried to set static member class Foo instance in another class , but this didn't solve the problem.
What is correct approach to do this. How to correctly implement signal handling in such case.
Here is example of my code
Code:
class MyContainer{
private:
std::vector<Foo> container;
public:
int removeFromContainer(Foo* aFoo) {
[Code] .....
I've been trying for more than one month to access a method found in a library called libcocosnas_static.a. I'm using Cocos2d-X version 2.0.4. The library has been used many times by my company to make games using cocos2d-1.0.1-x-0.12.0 without any problem.
This is what I've done:
1- I added the include paths of the library to both eclipse and Android.mk
2- Included the .h file using #include "NASPlatformUtil.h"
3- Added the libcocosnas_static.a file to the proj.androidobjlocalarmeabi folder
4- Added "LOCAL_WHOLE_STATIC_LIBRARIES += cocosnas_static" to the Android.mk file
5- Called the function using: NASPlatformUtil:: openUrl("http://xxx.xxx.com/");
I can right click on the function, click Open Declaration and get it without any problem, but the compiler keeps on giving me that dreaded error...
can we give the restriction to method for a particular class
View 1 Replies View RelatedI have a question similar to the one here: [URL] .....
The main difference is I would like to pass a method of derived class as a parameter to some other method in its template base class.
template <typename BaseType>
class Base {
public:
typedef void (Base::*Callback)(int A);
[Code] .....
The above is an example which does not compile. My compiler complains that the two BaseMethod() calls in DerivedMethod() are invalid uses of non-static member function.
Is this not possible to do, or is my syntax simply wrong? All I want is to be able to pass as an an argument to a method in the base class from the derived class some callback as a variable for the base class to invoke later.
I am need to compare 2 items from 2 separate drop down lists. These values are of int type. Say for example 2 drop down lists showing years. Now i need to find out these 2 years selected is greater or nearer to the current year.
I tried converting the values i receive from the drop down lists to int variables. But i am getting error then. I gave it like below.
int tmp = Convert.ToInt32(ddl_1.SelectedItem.Text);
this gives an error like this - Input string was not in correct format.
I need to know how to put an item from a drop down list to a variable of int data type.
I have designed a class called matrixType that has some overloaded operators (+, -, *, and <<); the arithmetic operator functions of which are overloaded as member functions of the class. As an alert mechanism, I want a message displayed when two matrices of dissimilar sizes are added/subtracted OR when two incompatible matrices are being multiplied. Displaying this error message is not the problem. However, I want a scheme where on detecting two matrices’ incompatibility, the operator function returns the error message (a string datatype) instead of what would be an erroneous result (the expected matrixType object).
In other words, what I may be essentially asking is: Is it possible for a function, say,
matrixType matrixType::operator+(const matrixType& otherMatrix) const
{
.
.
.
}
to return a dataType (like a string) other than the expected matrixType?
I have a class that is a template, I have to declare it in my main but i want the user to choose what type of data they will use in the class, I cant just declare myclass my, i have to use myclass<int> my, how can I change so user can select the proper datatype to use in the class.
View 5 Replies View RelatedI declared a member method to a class in its header file and implemented it in the cpp file. When I build and run the project in XCode, everything works fine. When I try to do it with a makefile, I get undefined symbols linker errors.
parser.h
Code:
#ifndef ENGINE_SCRIPT_PARSER_H
#define ENGINE_SCRIPT_PARSER_H
#include <string>
#include "variable.h"
namespace ninja_game_engine {
[Code] ....
The exact makefile looks like this:
Code:
test:
g++
Code/Engine/Modules/Timer/timer.cpp
Code/Engine/Modules/list.cpp
[Code] ....
I'm pretty sure that there is a weird namespace gotcha that I'm unaware of that LLVM (default OSX compiler) is compensating for that g++ isn't. Or maybe something weird with the optimization? I want the tests running at that level to make sure everything that is volatile is declared as such.
Rest of the code located here: [URL] ....
From my book:
"A static function might have this prototype:
static void Afunction(int n);
A static function can be called in relation to a particular object by a statement such as the following:
aBox.Afunction(10);
The function has no access to the non-static members of aBox. The same function could also be called without reference to an object. In this case, the statement would be:
CBox::Afunction(10);
where CBox is the class name. Using the class name and the scope resolution operator tells the compiler to which class Afunction() belongs."
Why exactly cant Afunction access non-static members?
I would like to avoid throwing things in constructors as much as possible.
Is this good design to have a static class method that checks arguments the caller will give to the constructor. The documentation of the class will say, thou shall call this method to validate thine arguments before calling the constructor, or else segfault may befall thoust.
I have function that returns historical data. I can access it, using file name. If I use file name, it reads that file and saves it to dictionary, so that in the future, if historical data is required for the same file, it does not read it again (it's lazy loading). If no file is supplied to the function, it tries to read file which is given in app settings.
However, for unit testing, I do not want to read any file. Instead, I want it to use small sample of hardcoded historical data. In order to do that, I think, I need to introduce interface to it. Then I can use some IoC to choose between different implementation for unit testing purpose and ordinary launch of application.
Function to get history is given as follows:
public static class Auxiliary
{
private static Dictionary<string, MyData> _myData;
public static MyData GetData(string fileName = null)
{
// ...
}
}
I have created default Unit Test project with Visual Studio so, as far as I know, by default it uses MSTest as test runner and MSUnit as unit testing framework but it does not have any IoC container so I should manage NuGet packages for solution and install Unity.
As far as I know, MSUnit (aka Moles) can unit test static methods (it's unconstrained isolation framework, like Typemock Isolator, unlike NUnit) but still many people suggest not to use any static methods for unit testing.
Should I use shim or stub [URL] Stubs should be used for faking external dependencies and here it is not external library, but my own code.
I would like to know if there's a way to make a method from a derived class a friend of its base class. Something like:
class Derived;
class Base {
int i, j;
friend void Derived::f();
protected:
Base();
[Code] ......
I have the following method:
public string[,] StringConvert_tblVFWPost(DataTable dt1) {
string[,] stringArray = new string[dt1.Rows.Count, dt1.Columns.Count];
for(int row = 0; row < dt1.Rows.Count; ++row) {
for(int col = 0; col < dt1.Columns.Count; col++)
[Code] ....
The error I'm getting is "Cannot implicitly convert type 'string[*,*]' to 'string'". So that tells me what the issue is. However, I'm not sure how to fix it.
How do I change this method, so that it properly returns my two dimensional array?
How to initialize a static member of a class with template, which type is related to a nested class?
This code works (without nested class):
#include<iostream>
using namespace std;
struct B{
B(){cout<<"here"<<endl;}
};
template<typename Z>
[Code] ,....
In my code, I wanted to write log exception to some file.
So I created a Utility class & wrote a static method in that to write log message to file.
public static void WriteLog(string message) {
using (FileStream fs = new FileStream(@"c:log.txt", FileMode.Append)) {
using (StreamWriter streamWriter = new StreamWriter(fs)) {
streamWriter.WriteLine(message);
}
}
}
This works fine so far but I fear if two methods call this function simultaneously.. what will happen? Also, I want to access this same Utility library in my other "WEB" projects... will it work there too?Or else.. what will be the best way to log exceptions in any project?
I sew lot of sample for ASP WEB API. In althose link the post method is using
HttpResponseMessage as return tyope
Is it possible to use return type bool in WEB API post method?
it seems everytime i use statics in a class i come across with a porblem.
this time i wanted to make a class i created static inside another class.
MainVariables.h file
static fge::window mWinMain;
if someone ever wants to reach it
MainVariables.cpp file
fge::window MainVariables::mWinMain;
...
...
fge::window MainVariables::GetWinMain()
{
return mWinMain;
}
but when i created some other MainVariables classes at other places instead of them reaching to the same window two window is being created.
yes i know maybe there are better methods but currently i m working on polymorphism and i need some static members.
I have 2 c++ libraries called "Lib1" and "Lib2". Both libraries have same class called "Total.h and Total.cpp"
I want to access lib1 class in lib2. since it has same class (i.e same header file), i couldn't refer lib1's members in lib2.
Lib 1 :
Total.h :
class Total {
public:
static void Test(double);
[Code] .....
How to access another library which has same name and how to add header file ?
Basically am trying to reference a method in a my Form1 class into another class. So far I have tried multiple different methods to try and access the method but have not managed it so far. Both classes use the same namespace. The part of my form class that I am using are as follows:
public class Form1 : Form
{
IContainer components = null;
private ButtonPanelThread p1, p3, p4;
[Code]....
When the below is done, does it call the constroctor only, and if yes, constructors do not have return types so how does it work? is there anything behind the scene?
wxAddHandler(new wxPNG_HANDLER);
and
sf::RenderWindow(sf::VideoMode(...),...);
Code:
class A {
public:
template<class T>
ObjectsBase* createObject(T* objType, Ogre::String name, Ogre::Vector3 position);
};
template<class T>
ObjectBase* A::createObject(T* objType, Ogre::String name, Ogre::Vector3 position);
My goal is to create any ObjectBase* derived object and return it to class A....
Code:
bool CObject<class T>::Create(LPDIRECT3DDEVICE9 pDevice, T *pMesh, float fAnimSpeed) {
if(pMesh == NULL)
return false;
m_pDevice = pDevice;
m_pMesh = pMesh;
if(m_pMesh->GetAnimController() != NULL) {
DWORD dwNumAnimSet = m_pMesh->GetAnimController()->GetMaxNumAnimationSets();
In here, T can be from the class CMesh * or CSkinnedMesh *, both have signature of GetAnimController
This maybe a flaw in my design... But I want Create to accept any kinds of meshes.
Should I create an interface or abstract class above all of those?
I have added an IMesh class that every derived classes has to have the needed interface.
But since then, I have another question, I feel that it is very cumbersome to include a template argument in every class/prototype that is associated with CObject
Say
bool CreatePlanesFromObject(CObject *pObject, D3DXPLANE *pPlanes);
Requires me to do this
template<class T> bool CreatePlanesFromObject(CObject<T>* pObject, D3DXPLANE* pPlanes);
Any ways I can omit the template argument, because it doesn't make sense to know the Mesh type in other classes...