C++ :: Static Variables In Classes

Mar 1, 2013

If I have a static variable in a class e.g. a pointer to another class like this: (B is another class)

class A {
public:
static B* cB;
};

Then I set that variable and create multiple instances of class A like this:

A::cB = new B;
As = new A[Number];

Then will the value of cB be the same across all instances?

I cannot pass the class pointer in the constructor as I need to create an array of instances. I tried this method but I get linker error.... unresolved external.

View 12 Replies


ADVERTISEMENT

C++ :: When To Use Static Classes

May 2, 2013

I am just wondering what the best practice is for when to use static classes (by static class, I mean a class which has only static attributes and functions).

If you are creating more than one independent object of a particular class, then obviously this should not be static because each object will be the same. But what about the case when you know that you will only ever need one instance of a class? On its own, does this mean that you should create it as a static class?

Personally, I use static class when I want its member attributes and functions to be available globally, which I think is fine. However, I am not sure about the case when I know that only one object will be created - should this be a static class or not?

View 1 Replies View Related

C++ :: Static Classes Vs Namespaces?

Jan 9, 2014

so i was trying to find out how to do unbuffered input in linux and came across this class: [URL] . i didnt like how i had to create a new instance of it each time i wanted to use it, so i made the functions static and renamed the class to Buffer. i could then call it like this: Buffer::On(); Buffer::Off();. My question is, when doing something like that where the class consists of two functions that can exist indepently of another, which is better: a class like what i did or wrapping it in a namespace?

View 3 Replies View Related

C++ :: Static Type Dispatch With Structs Inside Classes?

Oct 15, 2013

I wanted to ask if it is somehow possible to do:

Code:
class RaspberryPi: public Singleton<RaspberryPi> {
private:
static const DeviceInfo GPS;
template<typename Register_t>
auto ReadGPS(Register_t& Register) -> void

[Code] ....

There are two limitations I am facing:

First, it seems that anything that is part of a struct cannot be a compile-time expression. It's a nice way to group information, so it would be nice to have.

Secondly, it seems that all compile-time expressions cannot be inside a class (at least according to VC++), which means I have to move them to global level, but while it can be done, I don't really want to do it, because it's a platform detail.

In this case, static type dispatch would be nice to have because I have a function

Code:
template<typename Register_t>
auto ReadBus(Platforms::DeviceInfo Device, Register_t& Register) -> void {
switch (Device.Bus)

[Code] .....

There are two types of registers. With runtime dispatch, I get disgusting errors such as "could not deduce template arguments, blah blah" if some object doesn't have the required interface (i.e., don't have overloads for both register types). So the workaround would be to add two overloads and use something like asserts to stop invalid code from running, but it would be so nice to only allow correct code to compile and not get scary error messages.

View 5 Replies View Related

C# :: Can Use Variables From Other Classes

Feb 23, 2014

im creating an address book. One address book contains a ListBox, New User button, Edit User and Remove User button. The first form is suppose to allow you to view the users you've created on the ListBox and you can decide whether you want to remove it, create a new one or simply edit the user. Now The second form simply contains labels and textbox along with a save button. I'm having a bit of issue figuring out the ListBox. I want to be able to create a user and have the user be posted on the ListBox. I read that i must instantiate listbox then simply add it. Now on my form2 i have a for loop that loops through an Array of String were all the users will be created on. How can i call that array of string on to the form1?

Form 1:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

[code].....

EDIT:I just figured out that to call a variable from one form to another you simply instantiate the form then simply call it. PS. must be set to public:

ListBox1 createUser = new ListBox1();
createUser.userString[0];

why doesnt it show the windows when i run without debugging?

View 1 Replies View Related

C++ :: Static Vs Ordinary Variables?

Jul 26, 2014

Is it more expensive to use too many static variables instead of ordinary variables? If yes, then how?------------This is a topic given to me to find out about and I don't even know what are static variable except that they live throughout the life of program

and only disadvantage of using static variable instead ordinary variables in my mind is just they will use memory even when we don't need them

View 7 Replies View Related

C# :: How To Share Variables Between 3 Classes

Apr 2, 2014

I am wondering how to share a variable in 3 classes. For example:

variable class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

[Code] ...

class that edits the variable:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1

[code]......

The actual program:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1

[code]......

What should happen when this runs is this:

Variable changed in program: 0
Same variable changed in a different class: 1

Instead, this happens:

Variable changed in program: 0
Same variable changed in a different class: 0

View 7 Replies View Related

C :: For Static Variables When Memory Will Be Allocated?

May 21, 2013

For static variables when the memory will be allocated? During compilation or linking or loading time? In below program i am getting error :

(Error C2099: initializer is not a constant in microsoft visual studio) .

If i initialize x = 10 or any constant it works , why?

Code:
main() {
int i=10;
static int x = i;//error ?
if(x==i)
printf("Equal");

[Code] .....

View 8 Replies View Related

C++ ::  How To Overload Variables In Derived Classes

Apr 6, 2013

Is it possible to overload a variable in a derived class? Example:

struct Circle
{
int radius() const { return r; }
private:
int r;
}
struct Smiley : Circle
{
// inherits the function int radius() const, but doesn't return Smiley::r
private:
int r;
}

View 7 Replies View Related

C++ :: Using Global Variables In Static Library

Jul 22, 2014

Is it possible to use & change global variables in a Static Library? For example:

I declare a
bool test = true;
globally.

Then later in an exported function If the user wants, he can set that test to false. So the program later when checks test if it's true, will notice that it's not true, since one of my function changed it.

Is it right?

View 3 Replies View Related

C++ :: Bit-checking Across Multiple Classes Private Variables

Oct 6, 2013

I am writing a bit-check function just to make it easier on myself to check status flags in my classes. I use char variables and each bit represents something on or off. Since I have numerous classes that will use this functionality, it makes sense to write and compile the code only one time rather than for each class. I was thinking of writing the function and including it as a "friend" function to each class that needs it. Is that an appropriate way to do it?

View 2 Replies View Related

C++ :: Declare Static Variables In Header File

Oct 15, 2013

I read in another forum that it is bad practice to declare static variables in a header file? Is that true and if so why.

View 1 Replies View Related

C++ :: Changing Variables In Other Classes Code Blocks?

May 20, 2013

I want to make a basic RPG text based games with multiple classes. but I wan to know how In one class I make a variable and in another and can call the variable.

for exmaple

class2:
int hi = 1;
class1:
class2 a;
hi = 2;
cout << hi;
2

View 1 Replies View Related

Visual C++ :: Static Variables Local Or Global?

Mar 10, 2014

I came across the following code today and I was a bit surprised that it worked:-

Code:
std::string func_A () {
static std::string x;
if (!x.empty())
return x;

[Code] ....

I've simplified things slightly - but the basic point is that both functions are in the same source file and they both have a static std::string called 'x'. Being static, I guess they aren't (strictly) local variables. So how does the compiler know that they're different entities? Does it encode their signatures using the function name or something like that? If I call each function separately I do seem to get the correct string...

View 5 Replies View Related

C++ :: Segmentation Fault While Setting Classes Local Variables

Oct 12, 2014

When I set a local variable to a value it causes a segmentation fault. This happens in the GameObject class in the setGame method.

While your at it tell me what you think of the design. Am I on the right track? if not state why.

Here is the source: [URL]

View 6 Replies View Related

C++ :: Accessing Classes Member Variables Nested Inside Another Class

Feb 22, 2013

I have two classes, a Package class and a Person class. The Package class has two Person objects has member variables, a Sender and a Receiver. While overloading the << operator for the Package class so that it will make an output label from everything in the Package class. Here is my code...

class Package{
public:
Person Sender;
Person Reciever;
int weight;
double cost;
friend ostream &operator<<(ostream &out, Package &pack);

[Code] .....

So my problem is on that last output line, I am unable to call Sender.getName()... etc. Is there a proper syntax so that I can access the members of the Person class while overloading the << operator for the Package class?

View 2 Replies View Related

C++ :: Do Static Functions Have Access To Non Static Data Members Of A Class

Apr 17, 2013

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?

View 7 Replies View Related

C++ :: Accessing Non-static Members Inside Static Member Functions

Sep 11, 2013

What are the workarounds for accessing the non-static member variables of some class(Say A) inside static member functions of another class(Say B)? I am coding in c++. Class A is derived with public properties of class B. Any pointers?

View 7 Replies View Related

C# :: Static Method Inside Non-static Class

Aug 22, 2014

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?

View 2 Replies View Related

C++ :: Array Of Classes With Classes Inside

Oct 5, 2013

I have an array of (Student)classes created in Manager.h, which contains a new instance of class Name (name),(in Student.h)How would I go about accessing the SetFirstName method in Name.cpp if I was in a class Manager.cpp? I have tried using Students[i].name.SetFirstName("name");

// In Manager.h
#include"Student.h"
class Manager
{

[Code]....

View 2 Replies View Related

C++ :: Difference Between Static Local Variable And Static Global Variable?

Aug 5, 2013

Here is the code,

Code:
class A {
};
A& CreateObject() {
static A a;
return a;
} static A aa;
int main() {
return 0;
}

So is there any difference between a defined in CreateObject and aa?

View 6 Replies View Related

C++ :: How Do Two Classes Interact With Each Other

Aug 26, 2013

I want to know that how objects of two different classes interact with each other???

View 1 Replies View Related

C++ :: How To Know If Classes In Program Are Being Used

Aug 13, 2014

In this program the intention is to create a menu driven program for a pizza restaurant. I have to use a class called Pizza and have to include at least three public functions; one is called SetSize, another one is called Display, and the last one is called ComputePrice. A small pizza is worth $10, a medium is $14, and a large is $17. Each topping is worth 2 dollars. I know that the program runs correctly, but I have doubts over the classes and function actually being utilized correctly or at all.

Program:

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
class Pizza {
private:

int size;
int style;

[Code] .....

View 8 Replies View Related

C++ :: Using Multiset With Classes?

Oct 23, 2013

I'm trying to use multiset with a user defined class "edge". I'm trying to use the multiset as a priority queue, and I've created a "less<edge>" via operator<() overloading.

For some reason, I cannot insert edges into the multiset.

I understand that I might also have to create an "allocator". I got some ideas for creating it at [URL], but still don't know how to define size_type and difference_type.

Attached is my skeleton code, running on Windows 7 (32-bit), under Netbeans IDE, using Cygwin g++ 4.7.3.

How can I get this to work? What is important is that I get a priority queue working with my edges, prioritized by the weight.

#include <iostream>
#include <set> // for multiset
using namespace std; // assume std libraries (i.e. std::XXX)
class edge { // node, weight pair
public:

[Code].....

View 3 Replies View Related

C++ :: Why Vptr Is Not Static

Oct 14, 2013

So, even though the virtual pointer is constant across objects of class why cant it be made as static. Why each derived instance is having the pointer to vtable.

View 4 Replies View Related

C++ :: Derived Classes From DLL

May 5, 2013

I've created a base DLL for all my future DLL's, a way of getting version numbers and such and that compiles fine, but I can't add it into a class for a new DLL. All the headers do have an appropriate cpp to define the function declarations (and they compile fine).

All for the base DLL I have:

LibVer.h
Version.cpp
Function.cpp

LibVer.h

#pragma once
#include <vector>
#define DLLEXPORT 1
#define DLLIMPORT 2
#define DLL DLLIMPORT

[Code] .....

View 6 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved