C++ :: Scope Of References?
Apr 22, 2013
Did a little Googling on this but couldn't find anything definitive. Is it safe to do something like
Code:
void MyClass::myFunc(){
my_type_t &foo = some_obj->get_member_reference();
store_for_later(&foo);
}
Then at some pointer later in execution, another function uses the pointer passed to store_for_later.
View 5 Replies
ADVERTISEMENT
Jun 14, 2013
Are there any situations to explicitly use the scope resolution operator with global scope? I can imagine a situation like:
#include <cmath>
class IntWrapper{
public:
IntWrapper& pow(char);
IntWrapper(char);
private:
int m_int;
[Code] ....
But then I would think that the writer should have used a different name, and that using the scope resolution operator in the constructor body is still pointless...
View 8 Replies
View Related
May 28, 2013
I have the following code segment:
Code:
void Swap(Number& num1, Number& num2)
{
cout<<"Before swap:"<<num1<<" "<<num2<<endl;
Number& temp=num1;
num1=num2;
num2=temp;
cout<<"After swap:"<<num1<<" "<<num2<<endl;
}
[code]...
to which the output is:
Code:
Before swap:13 11
After swap:13 11
13 11 that seems confusing.
why doesn't Swap() swap the two Numbers?
View 8 Replies
View Related
Jan 11, 2014
I am trying to make a utility program for work that will update multiple projects with local dll references. Basically I work with two solutions (for talk sake solutIon1 and solutIon2). Generally solutIon1 will reference the dll's built In solutIon2 which reside on a server. However for debugging proposes I sometimes need to D/L the solutIon2 projects and build them local-ally, so that I can reference the solutIon2 dll's local-ally (this Is so that I can easily attach the dll and step Into the code). However this require changing the reference paths, so that I am pointing to the local-ally built dll's, which Is quite a laborious task.
So the question is how would I update references in solution1 from the program that I am making. I don't really know what to start reading about as I have never done anything like this before.
View 6 Replies
View Related
Jun 19, 2014
I am attempting to combine two vectors into a vector of pairs. I want to be able to alter the first and second of each pair and have those alterations reflected in the original vectors. I thought the following code might work but get compilation errors about a lack of viable overload for "=" for the line with the call to std::transform:
void f()
{
std::vector<int> a = {1,2,3,4,5};
std::vector<int> b = {6,7,8,9,0};
[Code].....
View 3 Replies
View Related
Sep 26, 2014
Is it permissible to declare, for example, `std::valarray<int&>`? If so, how do I initialize such if the `valarray` is a class member?
View 3 Replies
View Related
Dec 30, 2014
What rvalue references are? How are they useful? What are temporary objects?
View 1 Replies
View Related
Jun 18, 2014
I am checking to see if two references are bound to the same object. My instincts tell Me, "Check their addresses. If they match, they are bound to the same." At the same time, I have not found anything in the C++ standard which would support this approach. Am I missing something? Is there wording which backs up My instincts? Is there a standard function to do this?
View 4 Replies
View Related
Aug 20, 2013
I have this basic prototype:
struct int_wrapper{int i;};
template <const int_wrapper&... IPack>
void display_all(const int_wrapper&, IPack...);
But when I try to compile it, the compiler says IPack is not a type on the last line. Are packs of references not allowed?
View 5 Replies
View Related
Apr 3, 2014
I was wondering if there is way to convert a C# class to its own project and it automatically keeps its references.
View 3 Replies
View Related
Mar 6, 2015
Is it possible to create a class that stores (non-const) references to some objects and enables users direct access by using range-based for loops on them?
Code: class container {
public:
void add(int& value);
void remove(int& value);
...
};
int main()
{
container c;
for (auto& value:c) {
// `value' should be accessible as type `int&' instead of being a pointer, `std::reference_wrapper<int>' or something like that
}
}
View 6 Replies
View Related
Dec 14, 2014
I'm having some problems in understanding how the code below works and why it produces the output it produces.. What I'd expect is that both functions, namely `add_1' and `add_2', would print the same output; but I've been proven wrong :/ So why does the second one get different memory addresses for the same variable?
Code should be self-explaining:
Code: template<typename... Types>
void add_1(Types&&... values)
{
// by the way: why do i have to use `const int' instead of `int'?
std::vector<std::reference_wrapper<const int>> vector{
std::forward<Types>(values)...};
std::cout << "add_1:" << std::endl;
for (const auto& value:vector) {
std::cout << &value.get() << std::endl;
[code].....
View 4 Replies
View Related
Nov 15, 2014
I have an abstract class named Terrain, and a class named RoadMap, which supposed to hold an N*N array of Terrains. But I'm not sure what type should the RoadMap class hold:
Code:
#ifndef TERRAIN_H
#define TERRAIN_H
class Terrain {
[Code] ....
I can't use an array of refernces here, so I tried this:
Code: Terrain** terrain; and then I thought this was the way to go:
Code: Terrain (*terrain)[]; But now I'm not sure.
The N*N matrix size supposed to be determined according to a given input... What type should I use there?
View 2 Replies
View Related
Jan 28, 2014
#include "stdafx.h"
#include <iostream>
#include <math.h>
#include <time.h>
#include<iomanip>
#include<array>
#include <algorithm>
using namespace std;
const int AS = 6;
void FillingRandomly(int (*)[AS]);
void printing(int (*)[AS]);
[Code] ....
Basically I have to create an array, fill it, and then print it on screen. The tricky thing is that need to use pointers to fill it and print and later on sort it. My problem is that with this code is that i get
Error2error C2109: subscript requires array or pointer typec:userspcdesktopusbanthonydocumentsvisual studio 2012projectsessaieessaieessaie.cpp55
and
5IntelliSense: expression must have pointer-to-object typec:UserspcDesktopUSBAnthonyDocumentsVisual Studio 2012ProjectsEssaieEssaieEssaie.cpp55
Whenever I try to run it.
View 2 Replies
View Related
Aug 13, 2014
I am trying to understand RValue-references as return values of functions. First let's consider a simple function, that transforms a string into upper case letters.
const std::string
toUpper(std::string orig) {
std::transform(orig.begin(), orig.end(), orig.begin(), ::toupper);
return orig;
[Code] .....
It compiles, but I get the output 0 . Here I am wondering why the code above does not move the substr correctly while the code below does (prints out 1):
const std::string&&
no_sense(std::string abc) {
abc = abc.substr(1, 1);
return std::move(abc);
[Code] .....
In both cases abc is a temporary object inside of the function and gets deleted after the function is left. But why does the second version work and the first one does not?
cat.substr(1, 1)
And as my last question. Why doesn't
return std::move(abc.substr(1, 1));
work?
View 3 Replies
View Related
May 5, 2013
Why cant a dynamic memory allocation work with references? I was told that references work with const pointers deep down so shouldn't this be legal code?
int &&a=new int;
My compiler says that a entity of int* cannot be used to initialize a entity of int&&?
Does that mean that the compiler thinks of them as different types except deep down a reference is implemented with a pointer? Is this right?
View 14 Replies
View Related
Jan 21, 2015
I'm looking to implement a Database Access Layer for the project I'm working on, it's a mature project and I'm trying to simplify the database access and as far as possible and remove the Database logic from the Business logic.
Bringing in an ORM solution isn't an option at the moment so I'm looking at bringing in DAO objects to break the coupling. The problem I can't get around in my head is how to avoid Cyclic references
We currently have 2 projects
BL contains types such as Customer, Component and Product which need saving to the Database, the Database project can't know about these items or it would create the cyclic dependency.
I tried adding Dao items to the DB project to mirror these items and to also mirror the DB structure but that requires that the BL project knows how to convert between it's own types and the DAO types which is something I'd like to avoid.
I also tried inserting a third intermediate project that would control the conversion and saving, I called it my DAL project and tried adding functions that would take the BL item and perform CRUD operations but again I ran into the cyclic dependency issue.
My ideal solution would be that the BL project would just have to call a function along the lines of "SaveCustomer(Customer inCustomer)" and not have to worry about doing any conversion.
Is there a project structure that would allow for this?
View 1 Replies
View Related
Apr 8, 2013
Is there a point in dynamically creating a pointer during runtime only to dereference it? (if that is the right term, as in *pointer, depoint it?)
In this case should I perhaps store pointers instead of references?
Inventory.cpp
Code:
bool Inventory::addItem(InventoryItem& item) {
addItemAmount(item);
if (item.getAmount() > 0) {
if (hasEmptySlot()) {
addNewItem(*item.clone());
return true;
[Code] ....
Also I was wondering, is there some sort of built-in cloning functionality or do I have to write the clone functions myself? When creating new instances I see that I can either pass the constructor properties or a reference to an object of the same type.
For instance:
Code:
new InventoryItem(index, name....);
new InventoryItem(const InventoryItem&);
Is the second one casting?
View 14 Replies
View Related
Jun 11, 2014
Each of my header includes is protected by directives. I think I don't have to include Boolean in my work space because it is already included in the external dependencies section. and the Boolean.h is in the include path.
MachineShop, Boolean etc got undeclared identifier error
Tried to comment out the directives, to no avail.
Code:
#include <iostream>
#include <string.h>
#ifndef BOOLEAN_H_
# include "Boolean.h"
#endif
#ifndef PROCESS_H_
# include "Process.h"
#endif
#ifndef MACHINESHOP_H_
[Code] ....
View 2 Replies
View Related
Aug 16, 2012
Here's what I'm trying to do : A simple readout that shows the input/feedback values for 10 different sensors (i.e. a motor, a thermocouple, light sensor, etc).
What I got so far:
The data is stored in 2 different arrays:
One array is a 2D string array that stores descriptions, and won't be changed:
Sensor ID, Sensor Type, Input Signal, Feedback Signal
["A"]["Motor"]["PWM Signal"]["RPM"]
["B"]["Thermocouple"]["N/A"]["TempC"]
etc
The second array is another 2D int array that stores all the data values:
Input Signal, Feedback Signal
[0][0] // for Sensor A, Input is 0 PWM, 0 RPM read from sensor
[0][25] // for Sensor B, Input is 0, 25C read from sensor
etc
My question: I'd like to re-write the code to incorporate the new things I learned in c++. Right now, the descriptions for all 10 sensors are in 1 array and the sensor values are in another array. If I use pointers to access the values, is there a performance difference between:
1. Keeping it as is, with 2 2d arrays
2. 1 big structure that has descriptions and sensor values for all 10 sensors (i.e. combining everything into 1)
3. 1 parent class, and 10 different objects for each sensor (i.e. splitting into 10)
View 2 Replies
View Related
Feb 21, 2013
I have done alot of googling for the scope resolution operator and Ive gained a bit of an understanding as to what it does i know it can distinguish between global and local variables, but I see it used to access methods/members of classes such as this example, why not just use a dot instead to access it?:
sql:: Driver *driver;
Why is the scope resolution operator being used here?
View 11 Replies
View Related
Dec 14, 2013
The situation is the following :
Code:
vector<int>& function(int a , int b){
vector<int> s(3000000);
vector<int> xxx(4);
return xxx
}
Not to board people with details but if i am returning the the reference to a vector xxx what happens to vector s. is it destroyed ?? it should be, but i don't see it on my memory map (memory is not released) . can this be or should i go and search for error on some other place.....
View 2 Replies
View Related
Mar 16, 2013
Can we use using declaration within name space scope? is it safe to use it?
I think we can use using declaration for class scope and function scope only.
View 9 Replies
View Related
Oct 9, 2013
how to solve this undeclared error when compiling this code. I assume it has something to do with scope.
C code - 47 lines - codepad
Code:
#include <stdio.h>
int main(void) {
struct bank_account {
[Code].....
View 3 Replies
View Related
Oct 29, 2014
main.cpp
#include <iostream>
#include "sushi.h"
using namespace std;
int main()
{
do {
......sushi go;
......string x; <----------------------------Declared x here
......cout << "wanna use a banana?" << endl;
[Code ....
Error reads: 'x' was not declared in this scope.
How do I fix this?
P.S The sushi class does not matter, that is all perfect. Also, the dots are to represent my tabbing to make it easier to understand.
View 2 Replies
View Related
Jan 10, 2015
For some reason my compiler says "rename not declared in this scope" .... Isn't it declared in iostream? Or is rename only for C not C++? And if it is only for C how do I rename a file in C++ then?
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char* argv[]){
char oldname[] = "RomeTW.exe";
[Code] .....
View 2 Replies
View Related