C++ :: Creating New Pointer For A Class
Aug 2, 2013
I'm currently reading the C++ Guide for Dummies
Anyway right now I'm working with pointers and classes, and when I create a new pointer for my class it looks like this...
Pen *Pointerpen = new Pen;
But in the book they threw in this...
Pen *Pointerpen = new Pen();
Can you actually designate memory space for a function? Or was this a typo on their part? It's never come up before and they didn't explain it.
View 5 Replies
ADVERTISEMENT
Feb 6, 2015
I create an instance of a base class (not derived class) and assign it to base class pointer. Then, I convert it to a pointer to a derived class and call methods on it.
why does it work, if there is a virtual table?
when will it fail?
// TestCastWin.cpp : Defines the entry point for the console application.//
#include "stdafx.h"
#include <iostream>
class B
{
public:
B(double x, double y) : x_(x), y_(y) {}
double x() const { return x_; }
[Code] ....
View 14 Replies
View Related
Jan 16, 2013
Please consider the following code :
#include <iostream>
using namespace std;
class superclass;
class subclass1;
class subclass2;
[Code] ....
As you can see I want to create a dynamically allocated storage of references to a parent class each of which can then point to a child class, how ever I do not know how to extract the child class out again from that array so i may access its variable b.
View 2 Replies
View Related
Jun 26, 2014
I have a class in my application that only needs to be created once, but the object needs to be available to all other classes in my application if necessary. Since declaring everything static can be restrictive (as I understand), I created a class like this:
class Foo {
// Data members
// Constructor/Destructor
// Functions
};
extern Foo* myFoo = new Foo();
And then the global variable gets deleted at the very end of the main method when everything is done:
#include "Foo.cpp" // (yes, I know this is normally bad, this is how I'm required to code)
int main() {
// do stuff
delete myFoo;
return 0;
}
These won't link, though, because I get undefined reference linking errors to myFoo wherever I use it. I'm pretty sure this means I'm creating a singleton wrong, but I'm not sure what I'm doing wrong -- there's no const conflicts and the pointer is properly initialized (to my understanding). If there's a better way to do this than extern, I'm completely open to it, as long as it's understandable and works.
View 9 Replies
View Related
Aug 19, 2014
I am attempting to implement function pointers and I am having a bit of a problem.
See the code example below; what I want to be able to do is call a function pointer from another pointer.
I'll admit that I may not be explaining this 100% correct but I am trying to implement the code inside the main function below.
class MainObject;
class SecondaryObject;
class SecondaryObject {
public:
[Code]....
View 10 Replies
View Related
Sep 20, 2013
Suppose I have two classes A and B so how to access object of class A in constructor of B's class as a parameter.
View 6 Replies
View Related
Sep 15, 2013
Create in the private section of the ServerGroup class a pointer to a dynamic integer array called servers.
#ifndef ServerGroup_h
#define ServerGroup_h
class ServerGroup {
public:
private:
int *ptr = new int; // needs pointer to a dynamic integer array called servers
};
#endif
View 2 Replies
View Related
Mar 21, 2015
In this book, item 3 is about never treat arrays polymorphically. In the latter part of this item, the author talks about the result of deleting an array of derived class objects through a base class pointer is undefined. What does it mean? I have an example here,
Code:
class B
{
public:
B():_y(1){}
virtual ~B() {
cout<<"~B()"<<endl;
[Code] ....
This sample code does exactly what I want. So does the author mean the way I did is undefined?
View 1 Replies
View Related
Nov 29, 2013
My program is a basic MFC AppWizard (exe) created project in VC++ 6. In MainFrm.cpp, I am trying to access some user defined CMyView member functions. However when I try to do the standard procedure to get the CMyView pointer in MainFrm.cpp, I get the " ... 'CMyView' : undeclared identifier" error. To resolve this, I add " #include myView.h " at the top of MainFrm.h which then produces the following errors:
Code:
myview.h(21) : error C2143: syntax error : missing ';' before '*'
myview.h(21) : error C2501: 'CMyDoc' : missing storage-class or type specifiers
myview.h(21) : error C2501: 'GetDocument' : missing storage-class or type specifiers
What do these errors mean? Is there a simple way to access CMyView member functions from CMainFrame?
View 8 Replies
View Related
Nov 15, 2014
I was trying to implement own vector class and wrote some code. Below is the code.
Code: #include<iostream>
#include<vector>
using namespace std;
template <typename T> class MyVector
{
T *mem;
int m_size,final_size;
public:
MyVector() : final_size(1),m_size(4)
{
[code].....
I have question on this function.
Code: myVecPush_back(T t){}
Here I was able to put elements upto any number of time, but I have allocated memory to only 4 elements in T *mem.
My question, why the program is not crashing when I tried to put elements more that 4?
Is since the variable is of type Template? Any specific reason for this?
View 2 Replies
View Related
Feb 16, 2014
I am currently working on a "bag" class which is sort of a common sense answer to creating a random class with difference functions. I am attempting to create a "union" function which takes two bags ie: bag1 and bag2, adds all the items in both bags and creates a new bag ie: "bag3". For some reason I keep coming up with problems instead of solutions. Maybe it's the fact I just got done with 2 days of calculus. I don't know. My code is below. Both a main(source) and header file.
Header
#ifndef BAG_H
#define BAG_H
const int BAG_CAPACITY = 20;
template <typename T>
class Bag {
private:
int count; // Number of items in the Bag
[code]....
View 1 Replies
View Related
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
May 16, 2013
I've just recently moved from Visual Basic 6 over to C++ and decided that I would try and write a class to mimic the Get/Set behavior seen in a variety of other programming languages. So instead of just using an overloaded function, i.e:
Code:
class NoProp {
LPCWSTR m_Title;
public:
void Title(LPCWSTR NewValue) {m_Title = NewValue;};
LPCWSTR Title() {return m_Title;};
[code].....
So far this code only works with the LPCWSTR type (const wchar_t*). Unfortunately I got to this stage using only that type, and now when I try another type, such as int, it fails. When using the int type I believe it creates an error because the constructor of InitProp expects a type of pointer to be parsed. When I try using it with int* type (the private variable then becoming int **m_Variable) it compiles, thought I cannot access the property like a normal int.
My best guess from here is that I probably have to overload the InitProp constructor in a way that it can setup the Property classes for base-types and pointer-types, and then also modify the Property class to handle both.
View 6 Replies
View Related
Jun 28, 2012
I'm trying to implement a class hierarchy and a wrapper class with a pointer to the base class. The base class has operator< overloaded and the implementation makes use of virtual functions as some of the logic for sorting is in the derived classes. Unfortunately, when trying to use the base class operator< from the wrapper, I get a "pure virtual method called".
Below code is meant to illustrate my problem. Unfortunately it crashes on me (upon destruction of vec) and I cannot quite see, why. So two questions:
1. spot the error I made in the code below (having lived in Java-land for the last 5 years, I'm sure I just did some stupid error)?
2. How can I implement Wrapper::operator< to use Base::operator<? I know I could write a function and pass it to sort but I'm interessted if there is a way to actually use Base::operator<.
Code:
#include <vector>
#include <algorithm>
#include <cmath>
[Code].....
View 3 Replies
View Related
Oct 3, 2012
I have encountered a problem I can't see to solve. I want to access a function and can't seem to find the right combination to get me there. Here is what I am looking at:
CFoo1::CFoo2::GetStrDataC(int nRow) const
How do I call the GetStrDataC function from another class?
View 6 Replies
View Related
May 12, 2013
I have defined a class in a header file; just the class, no templates involved. I have a program where I'm reading in data in string format. Each string consists of a word, a delimiter, and a variable name. Example:
cajun/mustard
I want to take that string and make it the variable name of that class type. It would be implemented along the lines of:
Code:
string str;
//read/process string here, get:
str = "mustard";
createName(str);
//pass string to creator function When the function is called, I should get the variable:
Class mustard;
Thing is, I'm not supposed to know beforehand what the variable names are, only that I create them as they are read in. It could be mustard, it could be Maynard_James_Keenan, it could even be bazinga.
My problem is, what do I do for createName()? I've looked into the concepts of pairing, Factory implementation, and maps, but I don't think they answer my question.
(P.S. if I run into the same variable name being read in twice, what steps can I take to make sure that a duplicate variable isn't created? Do I need to add in code, or does the compiler know to watch for multiple variables of the same name?)
View 6 Replies
View Related
Jul 3, 2013
Creating the methods for class List
main.cpp Code: #include "List.h"
int main( )
{
List la; // create list la
la.push_front( "mom" );
la.push_back( "please" );
la.push_back( "send" );
la.push_back( "money" );
la.push_front( "hi" );
cout << "
la contains:
" << la << '
[code]...
View 12 Replies
View Related
Oct 17, 2014
I've been given specific instructions to create an array inside a Class Matrix using a constant n. This is my class but I am getting errors. I thought that maybe I had to initialize the const and the array using the constructor function Matrix() instead of directly in the class, but I didn't have any luck with that either.
class Matrix
{
public:
Matrix();
private:
const int n=3;
int e[n][n];
};
View 4 Replies
View Related
Sep 16, 2014
I recently posted a question related to creating a heap template class. The ultimate goal is to create a series of classes that serve a purpose that was overlooked in the Qt library that I need for my current project.
The current "end goal" is a PriorityQueue template that uses a comparer class which is inherited from a "template interface". Basically a pure virtual class template. Perhaps that is the mistake to begin with but hopefully not. (Is this a valid approach?)The problem I am getting is that when I compile the code, it says my derived comparer class is abstract.
I will include all related classes here. I doubt it is relevant but the templates and the classes based off them are in different namespaces.Here is the comparer "template interface":
// in global namespace
template<class T>
class IIMQOBJECTS_EXPORT IQComparer {
virtual int compare(T& a, T& b) = 0;
virtual bool equals(T& a, T& b) = 0;
virtual bool isGreaterThan(T& a, T& b) = 0;
virtual bool isLessThan(T& a, T& b) = 0;
};
Here is the class that is supposed to be non-abstract but isn't recognized as such:
// in the application namespace AND IN SAME project that has the NetEventInfo class
// all functions ARE defined/implemented in a cpp file
class APPCORE_EXPORT NetEventInfoComparer : ::IQComparer<NetEventInfo*> {
public:
NetEventInfoComparer();
~NetEventInfoComparer();
[code].....
View 6 Replies
View Related
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
Mar 21, 2015
The only difficulty im having is creating a class and methods & being able to access them from my win form app. Can i get a few tips on the do's and donts of creating classes / methods and accessing them from form app.
This is what i have put together so far.
public partial class Form1 : Form {
private Image[] dicePics;
private int[] diceNum;
private Random randomize;
public Form1() {
InitializeComponent();
[code]....
View 3 Replies
View Related
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
Feb 18, 2015
I am creating a class called time and we've had to do operator overloading for <, > , <=, >=, ==, !=, ++, --, >>, <<, * , +, and -.
Well I have done and error checked them all. The only one I cannot seem to get right is the minus and its because of the error checking. I am having issues with times like this
t1 = 0:0:2:3
t2 = 0:0:1:4
t1 - t2 should equal 0:0:0:59 but it returns 0:0:1:-1.
(days:hours:minutes:seconds)
I need it to check for all cases and I just do not know how. Here is the code I have so far:
time operator- (const time& x, const time& y){
time subtract;
subtract.days = x.days - y.days;
subtract.hrs = x.hrs - y.hrs;
subtract.mins = x.mins - y.mins;
subtract.secs = x.secs - y.secs;
[Code] .....
View 1 Replies
View Related
Apr 25, 2014
I have the following problem on my C++ Program and I am not sure why it is not working. I am creating a infix to postfix program through an inherited class which I am not sure it is working.
#include <iostream>
#include <stack>
using namespace std;
int in_stack_Priority(char a){
if(a == '*' || a == '/')
return 2;
[Code] .....
View 3 Replies
View Related
Apr 11, 2014
I'm using Visual C++ 2010 Express on Windows 8. What I want to do at the moment, is to create a class to make WinHttp requests easier. This is the code that I'm basically using to build the class: [URL] ....
This is what I got so far. (Completely untested). Example of how I hope that it will work:
HttpRequest MyRequest;
MyRequest.Initialize("Mozilla/5.0 (Windows NT 6.2; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0","","");
htmlCode = MyRequest.SendRequest("www.cplusplus.com","GET","");
cout << htmlCode << endl;
The code:
#include "stdafx.h"
#include <windows.h>
#include <winhttp.h>
#pragma comment(lib, "winhttp.lib")
class HttpRequest {
[Code] ......
View 6 Replies
View Related
Jun 24, 2014
I'm trying to create a program that will play scales at certain frequencies, but my arrays are not initializing correctly. I've read up to double check what i'm doing but it doesn't seem to be working. My only guess is my use of a global variable.
const int NotesInScale = 8;
class Scales {
private:
//all Major Scales
int CMajor[NotesInScale];
int GMajor[NotesInScale];
[Code] ....
i'm simply trying to put the frequencies in the scales (the numbers in the array) but I keep getting an error. I feel its a simple fix but i'm not seeing it.
View 8 Replies
View Related