variable-initialization

Understanding Delphi Variable Declarations/Initializations

北慕城南 提交于 2021-01-27 10:20:32
问题 As it pertains to Delphi... When a variable is declare of a certain type, is it initialized to an OBJECT of that type? Or must the variable be assigned an expression which returns an object of that type? I'm coming from a strong Java background. What I mean to ask is this... In Java, say you declare an instance variable of a user defined type named Orange. Which would look like this: private Orange _fruit; The variable _fruit still holds a reference to null until actually assigned an instance

C: Cannot initialize variable with an rvalue of type void*

大兔子大兔子 提交于 2020-06-09 11:14:37
问题 I have the following code: int *numberArray = calloc(n, sizeof(int)); And I am unable to understand why I receive the following error. Cannot initialize a variable of type 'int *' with an rvalue of type 'void *'`. Thank you. 回答1: The compiler's error message is very clear. The return value of calloc is void* . You are assigning it to a variable of type int* . That is ok with a C program, but not with a C++ program. You can change that line to int* numberArray = (int*)calloc(n, sizeof(int));

Why can't I access a member of this class? [duplicate]

最后都变了- 提交于 2020-01-30 05:57:05
问题 This question already has answers here : My attempt at value initialization is interpreted as a function declaration, and why doesn't A a(()); solve it? (5 answers) Closed 6 years ago . I have the following three class definitions: class String { public: String() {} String(const char *) {} }; class ClassA { public: ClassA(const String &) {} }; class ClassB { public: ClassB(const ClassA &, const String & = String()) {} void method() {} }; Now suppose I want to create an instance of ClassB :

Variable initialization (pointer and value)

做~自己de王妃 提交于 2019-12-31 01:45:12
问题 Foo f1 = Foo(); // (1) Ok Foo f2 = Foo; // (2) Compiler error Foo *p1 = new Foo(); // (3) Ok Foo *p2 = new Foo; // (4) Ok. Why?? I was wondering why there exists two ways to initialize pointers. It looks a little inconsistent. Is there some logical reason, and if so, what? Or, maybe it's some kind of legacy? And if so, What is the origin of such notation? 回答1: It's a bit... complicated, to say the least. When dealing with objects, both notations are equivalent. When dealing with primitive

variable may not have been initialized

落花浮王杯 提交于 2019-12-20 06:18:16
问题 inside of the arrayAverage method, avg has the right value (I tested it by puting println (avg) inside of the method. When i call the method from my main method and then print avg, netbeans tells me that the variable may not have been initialized. I must be doing something wrong, but I cannot figure out what. /*****************************************************************************\ * randomIntCount | * program generates a list of random numbers from 1-10, calculates | * the frequency of

Declaring a variable of user-defined type for later initialization

旧巷老猫 提交于 2019-12-13 22:03:23
问题 I want to create a global variable called process without assigning anything to it in a first moment. Later on I'll spawn a new process in the operating system, and assign it to that variable. It can be done in C# like so: class TestCS { // creating a variable private System.Diagnostics.Process process; private void SomeMethod() { // assigning a newly spawned process to it process = Process.Start("file.exe", "-argument"); process.WaitForInputIdle(); } } I wrote the code below to accomplish

How is this possible to use in c++?

牧云@^-^@ 提交于 2019-12-12 09:30:18
问题 To my surprise, I found that the name of a c++ object can be the same as class name. Can someone explain to me the reason why? When I declare an object of class a as a a1() , it does not raise an error, but doesn't call the constructor. Why is this happening? My code: #include<iostream> using namespace std; class a { public: a() { cout << "in a\n"; } }; int main() { a a1(); a a; } 回答1: When you write a a1(); it is actually being parsed as a function declaration not a call to the default

variable might not have been initialised

▼魔方 西西 提交于 2019-12-11 13:55:58
问题 I'm a relatively new student learning Java programming, but I'd like to ask for some help. The error I'm receiving in my code is stating "variable romanNumeral might not have been initialised." The intent for this program is for a user to enter a number from 1-39 and then have the appropriate roman numeral value displayed to the user via a dialog box. The code is not complete yet, as I've yet to find a solution to this problem due to the application not letting me compile my code. Here is the

Why this program that performs invalid pointer initialization compiles fine in C?

坚强是说给别人听的谎言 提交于 2019-12-10 20:39:42
问题 I wrote a simple C program and I was expecting that it will fail in compilation but unfortunately it compiles and runs fine in C, but fails in compilation in C++. Consider below program: #include <stdio.h> int main() { char *c=333; int *i=333; long *l=333; float *f=333; double *d=333; printf("c = %u, c+1 = %u",c,c+1); return 0; } Visit this link: http://ideone.com/vnKZnx I think that this program definitely can't compile in C++ due to C++'s strong type checking. Why this program compiles in C

Java String initialization

久未见 提交于 2019-12-08 15:26:53
问题 Which do you prefer and why" String myString = null; if(someCondition) myString = "something"; else myString = "something else"; OR String myString = ""; if(someCondition) myString = "something"; else myString = "something else"; I know that using the ternary (? :) operator is possible but I'd like to know about the above two. 回答1: Neither. Instead, this: String myString; if (someCondition) myString = "something"; else myString = "something else"; In both of your alternatives, the variable is