C++ :: Why Would A Class Allocate An Instance Of Itself

Oct 4, 2014

I'm currently learning the Qt framework and doing my first tutorial. Straight away I saw something that baffled me:

notepad.h

Code: namespace Ui {
class Notepad;
}
class Notepad : public QMainWindow

[Code] ....

Note the ui pointer and the heap allocation in the class constructor; I can't wrap my head around why one would do this. What's going on here?

View 5 Replies


ADVERTISEMENT

C++ :: Class Function That Uses Instance Of Its Child Class As Argument

Mar 1, 2013

I am facing a real-life problem, it can be simplified as below:

#include <iostream>
using namespace std;
class B;
class A {
public:
void f1(A a) {}
void f2(B b) {}

[Code]...

There is no problem at all with the f1(), it compiles and executes without any problem. But f2() gives compilation error. How to solve this?

The error message is: error: 'b' has incomplete type This is just to define the function f2() in a class, that uses an instance of its child class as one of its arguments.

View 11 Replies View Related

C++ :: Instance Of Class A Vs Class B Derived From Class A

Jul 3, 2014

There are two ways to access the members of class A inside class B:

1) Making an instance of class A in class B
2) Deriving class B from class A

So what is the basic difference in both ways as we can do same kind of work with both ways?

View 1 Replies View Related

C++ :: Declaring New Instance Of A Class

Nov 6, 2014

I am putting a instance o the Vehicle Class inside the constructor of the Calculate Class then calling it later. I get a warning saying the variable is not used and a error when I try to used the functions from the vehicle class saying use of undeclared identifier.

Code:
#include <iostream>
#include "Calculate.h"
#include "Vehicle.h"
#include <fstream>
Calculate::Calculate(){

[Code] ....

View 9 Replies View Related

C++ :: Vector Is Different For Every Class Instance

Feb 12, 2014

Okay so I have a class Student, which takes a number and a vector as a parameter for the constructor. Everything works well, until I output the values of the vector for every instance. The problem is that the same vector is being shared with EVERY instance I create, but I want it to be unique for every single one!

//Student.h
#ifndef __Grade_calculator__Student__
#define __Grade_calculator__Student__
#include <iostream>

[Code].....

View 1 Replies View Related

C++ ::  How To Have A Class Return Instance Of Itself

Nov 24, 2012

I was wondering if (in C++) you can instantiate a class (class foo) then have said class return the already instantiated object. (foo::instance())

In other words, can I have a class return it's-self via it's own methods? I want to be able to create a class (i.e. class foo) early in my program so it is already setup and ready to go. Then, farther down the line, I want to be able to call functions from that class without having to pass that object as an argument to my calling function. Can I do something like so:

MyClass::ReturnSelf()->foo();
or
MyClass::ReturnSelf().foo();

View 13 Replies View Related

C/C++ :: Creating New Instance Of A Class?

Mar 2, 2014

I have a background in c# and am very frustrated with c++. If I created a class in c# like so:

public class Memory{
int x = 0;
int y = 0;
int height = 0;
int width = 0;
string firstname = "Bob";
string lastame = "Chester";
}

and then created a new instance of this class from a separate class by doing:

public class Main{
Memory mem = new Memory();
}

I have raked the internet for a way to create a new instance of a class in c++ while keeping its default values and have come up empty handed.

View 13 Replies View Related

C++ ::  Passing Class Instance To Function

Jan 17, 2014

I know that it is possible to pass a class instance to a function, but in my experience, if said function changes any variables of the class, they don't actually get changed. For example, we have class object, that has a member int number = 5. Lets say we have two functions, func1() and func2, which are not members of class object. If we pass object to func1() which, lets say, increases number by 5 (so now number = 10), at the end of that function number still = 5.

Is there a way to bypass this and have functions alter class variables permanently?

I know that I can pass variables by reference, but, in my experience, such a thing does not work with vectors (which I am also dealing with), so simple passing the desired variables by reference won't work.

View 5 Replies View Related

C++ :: Cannot Create Struct That Contains Instance Of A Class

Sep 4, 2013

I'm trying to learn as much C++ as I can. I was writing a program that mixes linked lists and classes. There is the class "Obj" which only holds an integer called 'data' and the classic "struct node" structure for linked list, but this time the "node" structure will hold an instance of "Obj" Class and the next* pointer.

#include <iostream>
using namespace std;
class Obj {
private:
int data;
public:

[code]....

View 2 Replies View Related

C++ :: Finding A Specific Instance Of A Class

May 22, 2013

So I have a class that is like this:

class card {
public:
int id;
int val;
};
card card1;
card1.id = 1;
card1.val = 2;
card card2;
card2.id = 2;
card2.val = 45;

etc...

So my question is firstly, is there a better way to implement this? (a vector of classes or something maybe?) and how can I call up a specific instance of the class. For example, if I want the val of a specific instance of the class, how best can I do that?

View 3 Replies View Related

C++ :: Change Value Of Instance Variables From Another Class?

May 6, 2014

I'm trying to change the values of some instance variables in my Controller Class so that when the user inserts values into main class it changes for Controller.

class Controller {
public:
Controller();
~Controller();
double PCFreq;
__int64 CounterStart;

[Code] ....

The user should be able to choose which foo the want to use. So I create an object of controller in main like this

Controller* con = new Controller()

Now my issues is, when I take user input (an integer) and try to do this

con->choice1 = choice1;

only the object of con's choice1 is = to user input.

However back at the class for Controller, choice1 hasn't received a value.

I can't initialize through Controllers constructor because I get the user input through a switch statement and the range of con would only be as far as the case.

View 2 Replies View Related

C/C++ :: Allow User To Create A New Class Instance?

Dec 4, 2014

I am creating a program that allows a user to create multiple 'sequences' and multiple 'filters' and then apply a filter to a sequence.

Each sequence and filter is an array of values.

How do I go about allowing the user to create a 'new' sequence and then store the location so I can access it again later? Once they have created a new sequence they can go on to create another sequence and ten maybe a filter and then another sequence etc etc .. and then they can select sequence 1 and edit the values if they so wish.

They would be asked how many sample values for the sequence, and then I would create a sequence with that many values and an id (1,2,3,4...). They could then enter this id to view/edit the sequence.

The entering/editing values part I am fine with. I just don't know how to allow them to create multiple new instances of a class without using an array so something like..

sequenceClassName somearray[10];
int i;
*create a new array*
somearray[i].create_class(how_many_samples)
i++ //so next sequence they create is 2,3,4.. etc

- this then calls the member function that creates an array using 'sample_values = new float[how_many_samples]' and the user can input their data and edit it whenever by entering the id which will correspond to the somearray[i].

However that approach only allows them to enter a maximum of 11 sequences. It all depends on how big I make that initial array and it just seems like the wrong way to do it.

( how to interact with them, just how to create multiple classes and recall them later to access the data!)

View 2 Replies View Related

C++ :: Create 5 Instance Object Of Class Test?

Apr 27, 2014

I am new to C++. I am trying to create 5 instance object of class Test. I wonder if these two codes are the same.

Test testArray[5];
//which one is the correct way or what is the correct way?
for(i=0;i<5;i++)
{
Test testArray;
}

View 2 Replies View Related

C++ ::  How To Create Global Object / Instance Of A Class

Aug 25, 2013

I'm new in object oriented programming. I need creating a global object/instance of a class, that can be used by any function.

View 19 Replies View Related

C# :: Creating Instance Of A Class Given Its Assembly Object

Jan 15, 2015

Let's say I reference a dll in my project that has an abstract class in it:

public abstract class Module {
public string type;
public abstract string doSomething();
}

And then I have an Assembly object, loaded from a dll that is not referenced in project, but rather loaded during run-time, containing:

public class Tester : Module
{
public static string type = "Test";
public override void doSomething() {
//Stuff being done
return "hello";
}
}

How can I get the value of "type" in the class that is loaded during runtime? How do I use the "doSomething" method of it and get the returned object?

View 8 Replies View Related

C# :: Using Different Instance Of Class Depending On User Input?

Jan 13, 2015

Here's the problem I want to intialise different classes based on user input. I've tried using an IF statement but the scope of an if statement would mean the rest main program wouldn't be able to see it.

View 12 Replies View Related

C++ :: Template Class Instance - Proper Datatype

Oct 20, 2012

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 Related

Visual C++ :: Creating Instance Of MFC Derived Class

May 23, 2013

I need to create an object of a mfc derived CFormView class that's not in the doc/template (a second view class). But it was generated with a protected ctor. Here's the code explanation with comments.

I'm thinking all the normal classes of the Doc/View template are created starting with this code, but within the template code base.

Code:

CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(IDR_MAINFRAME,
RUNTIME_CLASS(CViewSwitchDoc), //<-expands to-> ((CRuntimeClass*)(&CViewSwitchDoc::classCViewSwitchDoc)),
RUNTIME_CLASS(CMainFrame), // main SDI frame window
RUNTIME_CLASS(CViewSwitchView));

But I have generated "another view" using the "Add Class" Wizard, it's a derived class of mfc CFormView which I named ViewForm. However I'm having a problem creating an instance of it because of the generated protected ctor and pulls a compile error of not being able to access ctor. Below are the header and implementation files of this said ViewForm class. How to create an object of this view ? Did I go about it all the wrong way since it's not in the doc/template group ?

// ViewForm.h file
#pragma once
// ViewForm form view
class ViewForm : public CFormView {
DECLARE_DYNCREATE(ViewForm)

[Code] ....

View 5 Replies View Related

C++ :: Global Class Instance In Multi-File Project

May 3, 2013

I'll just tell you in short how my Problem looks like: I should implement a New Class in a SourceCode i didnt write myself. The source code is extremely sized (i think approx >100.000 Lines of Code), so i dont want to change too much in it in order to get my Implementation done.

MY problem looks simplicified like that: Starting from 3 classes and my new class the pseudo-code looks like that:

Class1(){
float* m_CalibX, m_CalibY;
.. }
Class2(){
char* m_ImageData[];

[Code] .....

So, i need Parameters from 3 different classes to insert in my NewClass. The 3 Classes dont know anyting about each other. So, i need to implement a Class-Instance from Type NewClass which is known by the other 3 Classes. I did solve it in this way:

//ClassInstance.h

#include "NewClass.h"
static NewClass ClassInstance

I just wrote a headerfile with a class-instance which is getting included by the other 3 Classes. So they all know the same Instance and writing their Parameters into it. Is this a decent solution or could it happen to get bugs/ logical mistakes with it?

View 2 Replies View Related

C++ :: Polymorphism On Class Instance Is Not Directly Applicable To Array

Mar 19, 2013

Below code produces run-time segmentation fault (core dumped) when execution reached line 53: delete [] array

Code 1:

#include <cstdlib>
#include <iostream>
using namespace std;
#define QUANTITY 5
class Parent {
protected:
int ID;

[Code] ....

Output of code 1:

Constructor of A: Instance created with ID=1804289383
Constructor of A: Instance created with ID=846930886
Constructor of A: Instance created with ID=1681692777
Constructor of A: Instance created with ID=1714636915
Constructor of A: Instance created with ID=1957747793
A::showID() -- ID is 1804289383
A::showID() -- ID is 846930886
A::showID() -- ID is 1681692777
A::showID() -- ID is 1714636915
A::showID() -- ID is 1957747793
Try to delete [] array..
Segmentation fault (core dumped)

Question: Why does segmentation fault happen in code 1 above?

View 9 Replies View Related

C/C++ :: Simple Arkanoid Game - Deleting Instance Of A Class

Mar 24, 2015

I am making a simple arkanoid game. When the ball hits the block, i want the block to disappear. How would i go around this? i am working in visual studio. I got the collision and everything working.. Here is my code;

int main(int argc, char** argv){

//initialisation
SDL_Init(SDL_INIT_EVERYTHING);
SDL_WM_SetCaption("Graphics Window",NULL); //set name of project

//variable declaration
SDL_Surface*screen;
boolrunning;
intgameState;
float startTime;

[Code] ....

View 1 Replies View Related

C/C++ :: Making New Class Instance And Adding To Linked List

Oct 31, 2013

I have a .cpp file which contains 5 smaller defined classes. In my missile class I have a default constructor and a constructor that I invoke

class Missile{
private:  
bool isHuman;

[Code]...

My issue is when creating and adding the pointer object; it doesn't seem to create a new instance of the class-the Missile objects all share the same xPos value which is always the first xPos when the "fire" command is given. The "Missile *missile = new Missile(xPos, yPos, true);" line does not seem to create a new instance of the object with different variables but instead creates a new object with the same variables. Is it necessary for me to always make a separate .cpp and .h file for any class I want to create multiple instances of or can I keep the smaller classes in the same file and still create a new separate instance of the class?

View 3 Replies View Related

C++ :: File IO Inside A Class - No Instance Of Overloaded Function Getline Matches Argument List

Jan 24, 2012

Hey I am trying to use the getline() function to read a line from a file. For some reason Visual Studio 2010 gives me the following error. "No instance of overloaded function "getline" matches the argument list". The piece of code that produces the error is in a class in a separate .h file and is executed as a method of the object. I'm almost certain it has something to do with either the compiler thinking I am calling another getline in a different namespace or my parameters for the function are incorrect. Here is the code:

Code:
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
class InsultGenerator

[Code] .....

View 1 Replies View Related

C Sharp :: Create Instance Of Form Into Non Form Class To Access Button / Label

Nov 23, 2014

I have a non form class. I want to update label/ check status of check box etc.. in non form class ( here resides functions that contains logic). How can i do that ?

View 4 Replies View Related

C++ :: How To Allocate Memory When It Comes To Using Templates

Oct 25, 2013

I'm currently learning templates -- & my logic is in a knot with what I am trying to do which is the following:

-Create a function name load
-Accepts a filename (the filename is a text file of integers)
-Open the file
-Create an array(dynamically allocating an array) filling it with the elements read in from the file & returns the array(so that the return type of the array is a pointer to the element type of the array).

//Header file:
#ifndef BUBBLE_SORT_H
#define BUBBLE_SORT_H
#include <iostream>
template <typename T>
void load(std::string filename, T *&arr, int *size);

[code].....

how to allocate memory when it comes to using templates..

View 2 Replies View Related

C :: Function That Allows To Allocate Memory To Variable

Nov 8, 2013

I am trying to make a function that allows me to allocate memory to a "mem" variable and setting each of its chunk's status to FREE. FREE is defined as 0. Below is my code of the function.

Code:

int allocate(mem *mm, int num_chunks, int chunk_size) {
int i;
mem *temp;
if((mm = (mem *) malloc((num_chunks + 1) * chunk_size)) == NULL){
perror("Failed to Malloc

[code]...

mem; If my function works the way it should, it should print out five 0 because that is how I set them in the function, but this is not the case. I've looked at my function for 2 hours, but I could not figure out any logical error. Now, I think my problem lies with my limited knowledge of pointer arithmetic. On the other hand, when I insert 1000 as the second argument into my function, it gives seg faults, which is not the case for smaller values like 5, 10, 15, etc.

View 6 Replies View Related







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